<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/blog/atom.xml" title="稻草人的编程之路" type="application/atom+xml" />






<meta name="description" content="人生的意义，不在于最终获得了什么，而在于曾经努力追求过什么">
<meta property="og:type" content="website">
<meta property="og:title" content="稻草人的编程之路">
<meta property="og:url" content="https://wangxiaochuang.github.io/page/2/index.html">
<meta property="og:site_name" content="稻草人的编程之路">
<meta property="og:description" content="人生的意义，不在于最终获得了什么，而在于曾经努力追求过什么">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="稻草人的编程之路">
<meta name="twitter:description" content="人生的意义，不在于最终获得了什么，而在于曾经努力追求过什么">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://wangxiaochuang.github.io/page/2/"/>





  <title>稻草人的编程之路</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">稻草人的编程之路</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">众人皆醉我独醒 举世皆浊我独清</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/10/14/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/10/14/1.html" itemprop="url">mathjax</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-14T20:42:18+08:00">
                2018-10-14
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/工具/" itemprop="url" rel="index">
                    <span itemprop="name">工具</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/工具/markdown/" itemprop="url" rel="index">
                    <span itemprop="name">markdown</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/工具/markdown/mathjax/" itemprop="url" rel="index">
                    <span itemprop="name">mathjax</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/10/14/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/10/14/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/10/14/1.html" class="leancloud_visitors" data-flag-title="mathjax">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="mathjax"><a href="#mathjax" class="headerlink" title="mathjax"></a>mathjax</h1><p>由于在写博客的过程中经常要用到公式，本人记录该博客用于帮助自己熟悉公式表示的各种方式，同时如果能够帮助到大家，则更好</p>
<h2 id="hexo-next-mathjax"><a href="#hexo-next-mathjax" class="headerlink" title="hexo + next + mathjax"></a>hexo + next + mathjax</h2><p>在使用hexo + next的过程，我发现写的mathjax表达式根本就不生效，在网上也查询了各种资料，使用了复杂的方式使之生效。本人比较懒，没有去试验那么多，就切换了一下markdown的渲染引擎，默认为 <code>hexo-renderer-marked</code>，切换后的引擎为 <code>hexo-renderer-kramed</code>，然后就生效了</p>
<ol>
<li>切换引擎</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm uninstall hexo-renderer-marked --save</span><br><span class="line">npm install hexo-renderer-kramed --save</span><br></pre></td></tr></table></figure>
<ol>
<li>修改next主体的配置文件<code>_config.yml</code></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 开启 mathjax 支持，搜索mathjax即可找到</span></span><br><span class="line"><span class="built_in">enable</span>: <span class="literal">true</span></span><br><span class="line">per_page: <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 或者不对每个页面都启用mathjax，将per_page设置为false</span></span><br><span class="line"><span class="comment"># 在每个博客的头信息中加上 mathjax: true</span></span><br></pre></td></tr></table></figure>
<h3 id="markdown与mathjax的冲突"><a href="#markdown与mathjax的冲突" class="headerlink" title="markdown与mathjax的冲突"></a>markdown与mathjax的冲突</h3><p>由于hexo在渲染的过程中会把一些markdown的符号（下划线 <code>_，*，{，}，\\</code> 等）渲染为响应的html标签，而这些又是mathjax要用的，因此产生冲突</p>
<p>如何解决呢？</p>
<p>修改 <code>node_modules\kramed\lib\rules\inline.js</code> 文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1. 修改第11行</span><br><span class="line">//escape: /^\\([\\`*&#123;&#125;\[\]()#$+\-.!_&gt;])/,</span><br><span class="line">escape: /^\\([`*\[\]()#$+\-.!_&gt;])/,</span><br><span class="line">2. 修改第20行</span><br><span class="line">//em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,</span><br><span class="line">em: /^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,</span><br></pre></td></tr></table></figure></p>
<h2 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h2><h3 id="一个简单的例子"><a href="#一个简单的例子" class="headerlink" title="一个简单的例子"></a>一个简单的例子</h3><ol>
<li><p>行内（inline）公式 $d = \sqrt { (q_1 - p_1)^2 + (q_2 - p_2)^2 + {\cdots} + (q_n - p_n)^2 }$</p>
</li>
<li><p>独行显示公式</p>
</li>
</ol>
<script type="math/tex; mode=display">
d = \sqrt { (q_1 - p_1)^2 + (q_2 - p_2)^2 + {\cdots} + (q_n - p_n)^2 }</script><h3 id="一些特殊符号列表"><a href="#一些特殊符号列表" class="headerlink" title="一些特殊符号列表"></a>一些特殊符号列表</h3><ol>
<li>希腊字母</li>
</ol>
<div class="table-container">
<table>
<thead>
<tr>
<th>显示</th>
<th>命令</th>
<th>显示</th>
<th>命令</th>
<th>显示</th>
<th>命令</th>
</tr>
</thead>
<tbody>
<tr>
<td>α</td>
<td>\alpha</td>
<td>β</td>
<td>\beta</td>
<td>ν</td>
<td>\nu</td>
</tr>
<tr>
<td>ξ</td>
<td>\xi</td>
<td>γ</td>
<td>\gamma</td>
<td>δ</td>
<td>\delta</td>
</tr>
<tr>
<td>ε</td>
<td>\epsilon</td>
<td>ζ</td>
<td>\zeta</td>
<td>η</td>
<td>\eta</td>
</tr>
<tr>
<td>θ</td>
<td>\theta</td>
<td>ι</td>
<td>\iota</td>
<td>κ</td>
<td>\kappa</td>
</tr>
<tr>
<td>λ</td>
<td>\lambda</td>
<td>μ</td>
<td>\mu</td>
<td>π</td>
<td>\pi</td>
</tr>
<tr>
<td>ρ</td>
<td>\rho</td>
<td>σ</td>
<td>\sigma</td>
<td>τ</td>
<td>\tau</td>
</tr>
<tr>
<td>υ</td>
<td>\upsilon</td>
<td>φ</td>
<td>\phi</td>
<td>χ</td>
<td>\chi</td>
</tr>
<tr>
<td>ψ</td>
<td>\psi</td>
<td>ω</td>
<td>\omega</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<ol>
<li>特殊符号</li>
</ol>
<div class="table-container">
<table>
<thead>
<tr>
<th>显示</th>
<th>命令</th>
<th>显示</th>
<th>命令</th>
<th>显示</th>
<th>命令</th>
</tr>
</thead>
<tbody>
<tr>
<td>∞</td>
<td>\infty</td>
<td>∪</td>
<td>\cup</td>
<td>∩</td>
<td>\cap</td>
</tr>
<tr>
<td>⊂</td>
<td>\subset</td>
<td>⊆</td>
<td>\subseteq</td>
<td>⊃</td>
<td>\supset</td>
</tr>
<tr>
<td>∈</td>
<td>\in</td>
<td>∉</td>
<td>\notin</td>
<td>∅</td>
<td>\varnothing</td>
</tr>
<tr>
<td>∀</td>
<td>\forall</td>
<td>∃</td>
<td>\exists</td>
<td>¬</td>
<td>\lnot</td>
</tr>
<tr>
<td>∇</td>
<td>\nabla</td>
<td>∂</td>
<td>\partial</td>
<td>$\cdots$</td>
<td>\cdots </td>
</tr>
<tr>
<td>$\vdots$</td>
<td>\vdots</td>
<td>$\ddots$</td>
<td>\ddots</td>
<td>$\to$</td>
<td>\to</td>
</tr>
<tr>
<td>$\times$</td>
<td>\times</td>
<td>$\bar x$</td>
<td>\bar x</td>
<td>$\overline{xyz}$</td>
<td>\overline{xyz}</td>
</tr>
<tr>
<td>$\lceil x \rceil$</td>
<td>\lceil x \rceil</td>
<td>$\lfloor x \rfloor$</td>
<td>\lfloor x \rfloor</td>
<td>$\hat{y}$</td>
<td>\hat{y}</td>
</tr>
<tr>
<td>${n+1 \choose 2k}</td>
<td>{n+1 \choose 2k} 或 \binom{n+1}{2k}</td>
<td>$\leftarrow$</td>
<td>\leftarrow</td>
<td>$\cdot$</td>
<td>\cdot</td>
</tr>
</tbody>
</table>
</div>
<h3 id="修饰与分组"><a href="#修饰与分组" class="headerlink" title="修饰与分组"></a>修饰与分组</h3><ol>
<li>上下标</li>
</ol>
<p>使用 ^（上标） _（下标），如 <code>A_n^3</code> 结果为 $A_n^3$  </p>
<ol>
<li><p>各种括号</p>
<ol>
<li>小括号：<code>(a + b)</code> 结果为 $(a + b)$</li>
<li>中括号：<code>[a + b]</code> 结果为 $[a + b]$</li>
<li>尖括号：<code>\langle a + b \rangle</code> 结果为 $\langle a + b \rangle$</li>
<li><p>大括号： <code>\left( \frac{x}{y} \right)</code> 结果为 $\left( \frac{x}{y} \right)$</p>
<p> 可以对比一下 <code>( \frac{x}{y} )</code> 结果为 $( \frac{x}{y} )$</p>
</li>
</ol>
</li>
</ol>
<ol>
<li>分组</li>
</ol>
<p>分组是什么意思呢，比如根号下的所有内容就应该是一个组，再比如排列组合公式的上小数值就应该分别是两个组</p>
<ol>
<li>特殊函数</li>
</ol>
<p>每个函数都有一个对应的名字，使用 <code>\函数名</code> 的形式表示</p>
<div class="table-container">
<table>
<thead>
<tr>
<th>函数</th>
<th>表达式</th>
<th>结果</th>
</tr>
</thead>
<tbody>
<tr>
<td>求累和</td>
<td><code>\sum_{i = 1}^{n}{x_i}</code></td>
<td>$\sum_{i=1}^{n}{x_i}$</td>
</tr>
<tr>
<td>求累积</td>
<td><code>\prod_{i = 1}^{n}{x_i}</code></td>
<td>$\prod_{i=1}^{n}{x_i}$</td>
</tr>
<tr>
<td>求极限</td>
<td><code>\lim_{x \to 0}</code></td>
<td>$\lim_{x \to 0}$</td>
</tr>
<tr>
<td>求积分</td>
<td><code>\int_0^\infty{dx}</code></td>
<td>$\int_0^\infty{dx}$</td>
</tr>
<tr>
<td>分数除</td>
<td><code>\frac{x}{y}</code></td>
<td>$\frac{x}{y}$</td>
</tr>
<tr>
<td>求根式</td>
<td><code>\sqrt[x]{y}</code></td>
<td>$\sqrt[x]{y}$</td>
</tr>
<tr>
<td>sin函数</td>
<td><code>\sin x</code></td>
<td>$\sin x$</td>
</tr>
<tr>
<td>ln函数</td>
<td><code>\ln x</code></td>
<td>$\ln x$</td>
</tr>
<tr>
<td>max函数</td>
<td><code>\max(x, y, z)</code></td>
<td>$\max(x, y, z)$</td>
</tr>
<tr>
<td>min函数</td>
<td><code>\min(x, y, z)</code></td>
<td>$\min(x, y, z)$</td>
</tr>
</tbody>
</table>
</div>
<ol>
<li><p>字体</p>
<ol>
<li><code>{𝙰𝙱𝙲𝙳𝙴𝙵𝙶𝙷𝙸𝙹𝙺𝙻𝙼𝙽𝙾𝙿𝚀𝚁𝚂𝚃𝚄𝚅𝚆𝚇𝚈𝚉1234567890}</code><br> ${𝙰𝙱𝙲𝙳𝙴𝙵𝙶𝙷𝙸𝙹𝙺𝙻𝙼𝙽𝙾𝙿𝚀𝚁𝚂𝚃𝚄𝚅𝚆𝚇𝚈𝚉1234567890}$</li>
<li><code>\cal{𝙰𝙱𝙲𝙳𝙴𝙵𝙶𝙷𝙸𝙹𝙺𝙻𝙼𝙽𝙾𝙿𝚀𝚁𝚂𝚃𝚄𝚅𝚆𝚇𝚈𝚉1234567890}</code><br> $\cal {𝙰𝙱𝙲𝙳𝙴𝙵𝙶𝙷𝙸𝙹𝙺𝙻𝙼𝙽𝙾𝙿𝚀𝚁𝚂𝚃𝚄𝚅𝚆𝚇𝚈𝚉1234567890}$</li>
</ol>
</li>
<li><p>矢量符号</p>
</li>
</ol>
<p>\vec a 结果为 $\vec a$</p>
<ol>
<li>添加文字</li>
</ol>
<p>添加文字</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$$\left. \begin&#123;array&#125;&#123;1&#125;</span><br><span class="line">\text&#123;if $n$ is even:&#125;&amp;n/2 \\</span><br><span class="line">\text&#123;if $n$ is odd:&#125;&amp;3n+1</span><br><span class="line">\end&#123;array&#125; \right\&#125; = f(n)$$</span><br></pre></td></tr></table></figure>
<p>结果为</p>
<script type="math/tex; mode=display">\left. \begin{array}{1}
\text{if $n$ is even:}&n/2 \\
\text{if $n$ is odd:}&3n+1
\end{array} \right\} = f(n)</script><h3 id="多行公式"><a href="#多行公式" class="headerlink" title="多行公式"></a>多行公式</h3><p>代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">\begin&#123;equation&#125;\begin&#123;split&#125;</span><br><span class="line">y &amp;= \frac &#123;x^2 + 1&#125; &#123;x&#125; \\</span><br><span class="line">&amp;= x + \frac&#123;1&#125;&#123;x&#125;</span><br><span class="line">\end&#123;split&#125;\end&#123;equation&#125;</span><br></pre></td></tr></table></figure></p>
<p>结果如下：<br>\begin{equation}\begin{split}<br>y &amp;= \frac {x^2 + 1} {x} \\<br>&amp;= x + \frac{1}{x}<br>\end{split}\end{equation}</p>
<h3 id="空格"><a href="#空格" class="headerlink" title="空格"></a>空格</h3><p>如果我们自己输入空格，mathjax会忽略，那我们如何输入空格呢</p>
<ol>
<li><p>小空格</p>
<p> x\ y 结果为 $x\ y$</p>
</li>
<li><p>四空格</p>
<p> x\quad y 结果为 $x\quad y$</p>
</li>
</ol>
<p>也可以使用多个小空格</p>
<h3 id="使用矩阵"><a href="#使用矩阵" class="headerlink" title="使用矩阵"></a>使用矩阵</h3><ol>
<li>起始标记 <code>\begin{matrix}</code>，结束标记 <code>\end{matrix}</code></li>
<li>每行的末尾需要标记 <code>\\</code>，行间元素之间以 <code>&amp;</code> 分割</li>
<li>如</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$$\begin&#123;matrix&#125;</span><br><span class="line">1 &amp; 0 &amp; 0 \\</span><br><span class="line">0 &amp; 1 &amp; 0 \\</span><br><span class="line">0 &amp; 0 &amp; 1 \\</span><br><span class="line">\end&#123;matrix&#125;$$</span><br></pre></td></tr></table></figure>
<p>结果为：</p>
<script type="math/tex; mode=display">\begin{matrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{matrix}</script><ol>
<li><p>添加括号</p>
<ol>
<li>pmatrix: 小括号边框</li>
<li>bmatrix: 中括号边框</li>
<li>Bmatrix: 大括号边框</li>
<li>vmatrix: 单竖线边框</li>
<li>Vmatrix: 双竖线边框</li>
</ol>
</li>
<li><p>列向量</p>
</li>
</ol>
<p>代码如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$$</span><br><span class="line">(\theta_0 \quad \theta_1 \quad \theta_2) \quad \times \quad \begin&#123;bmatrix&#125;</span><br><span class="line">1 \\ </span><br><span class="line">x_1 \\ </span><br><span class="line">x_2</span><br><span class="line">\end&#123;bmatrix&#125; = \theta_0 + \theta_1 x_1 + \theta_2 x_2</span><br><span class="line">$$</span><br></pre></td></tr></table></figure>
<p>结果为</p>
<script type="math/tex; mode=display">
(\theta_0 \quad \theta_1 \quad \theta_2) \quad \times \quad \begin{bmatrix}
1 \\ 
x_1 \\ 
x_2
\end{bmatrix} = \theta_0 + \theta_1 x_1 + \theta_2 x_2</script><h3 id="阵列"><a href="#阵列" class="headerlink" title="阵列"></a>阵列</h3><ol>
<li>开始结束使用 <code>{array}</code> 声明</li>
<li>在 <code>{array}</code> 后以 <code>{}</code> 逐行统一声明对齐方式，左对齐（l），居中（c），右对齐（r），还可以插入竖线</li>
<li>插入水平线：<code>\hline</code></li>
<li>如下</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$$\begin&#123;array&#125;&#123;c|ccc&#125;</span><br><span class="line">&amp; &#123;姓名&#125; &amp; &#123;性别&#125; &amp; &#123;年龄&#125; \\</span><br><span class="line">\hline</span><br><span class="line">&#123;1&#125; &amp; &#123;张三&#125; &amp; &#123;男&#125; &amp; &#123;27&#125; \\</span><br><span class="line">&#123;2&#125; &amp; &#123;李四&#125; &amp; &#123;女&#125; &amp; &#123;35&#125; \\</span><br><span class="line">\end&#123;array&#125;$$</span><br></pre></td></tr></table></figure>
<p>结果为：</p>
<script type="math/tex; mode=display">\begin{array}{c|ccc}
/ & {姓名} & {性别} & {年龄} \\
\hline
{1} & {张三} & {男} & {27} \\
{2} & {李四} & {女} & {35} \\
\end{array}</script><h3 id="方程组"><a href="#方程组" class="headerlink" title="方程组"></a>方程组</h3><ol>
<li>开始结束使用 <code>{case}</code> 声明</li>
<li>例如</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$$\begin&#123;cases&#125;</span><br><span class="line">\alpha_1 x + \beta_1 y + \gamma_1 z = d_1 \\</span><br><span class="line">\alpha_2 x + \beta_2 y + \gamma_2 z = d_2 \\</span><br><span class="line">\alpha_3 x + \beta_3 y + \gamma_3 z = d_3 \\</span><br><span class="line">\end&#123;cases&#125;$$</span><br></pre></td></tr></table></figure>
<p>结果为：</p>
<script type="math/tex; mode=display">\begin{cases}
\alpha_1 x + \beta_1 y + \gamma_1 z = d_1 \\
\alpha_2 x + \beta_2 y + \gamma_2 z = d_2 \\
\alpha_3 x + \beta_3 y + \gamma_3 z = d_3 \\
\end{cases}</script>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/10/05/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/10/05/1.html" itemprop="url">机器学习之knn</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-05T20:40:00+08:00">
                2018-10-05
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/机器学习/" itemprop="url" rel="index">
                    <span itemprop="name">机器学习</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/机器学习/knn/" itemprop="url" rel="index">
                    <span itemprop="name">knn</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/10/05/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/10/05/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/10/05/1.html" class="leancloud_visitors" data-flag-title="机器学习之knn">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="K近邻算法（KNN）"><a href="#K近邻算法（KNN）" class="headerlink" title="K近邻算法（KNN）"></a>K近邻算法（KNN）</h1><p>什么是K近邻呢？它是机器学习中一个非常简单的算法，在理论上也是比较成熟的方法，计算与待评估指标最相近的K个数据，然后计算平均值</p>
<h2 id="熟悉数据"><a href="#熟悉数据" class="headerlink" title="熟悉数据"></a>熟悉数据</h2><p>这里使用 <a href="https://pan.baidu.com/s/1T46gPSDuPbogR16qeL3Z_w">airbnb 密码:0su1</a> 的房屋租用情况为例来学习KNN算法</p>
<ol>
<li>该数据集中包含有很多指标，这里我们只取其中几个指标</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">data = pd.read_csv(<span class="string">'listings.csv'</span>)</span><br><span class="line">features = [<span class="string">'accommodates'</span>, <span class="string">'bedrooms'</span>, <span class="string">'bathrooms'</span>, <span class="string">'beds'</span>, <span class="string">'price'</span>, <span class="string">'minimum_nights'</span>, <span class="string">'maximum_nights'</span>, <span class="string">'number_of_reviews'</span>]</span><br><span class="line">data = data[features]</span><br><span class="line">print(data.shape)       </span><br><span class="line">data.head()             <span class="comment"># 看下数据都有什么</span></span><br></pre></td></tr></table></figure>
<ol>
<li><p>指标释义：</p>
<ol>
<li>accommodates         可以容纳的旅客数量</li>
<li>bedrooms             卧室的数量</li>
<li>bathrooms            厕所的数量</li>
<li>beds                 床的数量</li>
<li>price                每晚的费用</li>
<li>minimum_nights       客人最少租了几天</li>
<li>maximum_nights       客人最多租了几天</li>
<li>number_of_reviews    评论数</li>
</ol>
</li>
</ol>
<h2 id="试验（基础篇）"><a href="#试验（基础篇）" class="headerlink" title="试验（基础篇）"></a>试验（基础篇）</h2><h3 id="提出问题"><a href="#提出问题" class="headerlink" title="提出问题"></a>提出问题</h3><p>假设现在我们有一个房子有3个房间，那么我们要租多少钱呢？思路：选择与我们的房间个数最近（可能是2个或4个房间）的K个数据，求平均价格</p>
<p>实现步骤：</p>
<ol>
<li>计算房间个数与我们自己的房间数3的距离（直接相减即为距离）</li>
<li>按距离从小到大排序</li>
<li>取前K个数据的价格求平均值即为预测结果</li>
</ol>
<p>这里提到了距离，但是说的是一个指标，我们的数据大部分情况下是多个指标的，那么如何来计算多个指标的距离呢？</p>
<script type="math/tex; mode=display">
d = \sqrt {(q_1 - p_1)^2 + (q_2 - p_2)^2 + {\cdots} + (q_n - p_n)^2}</script><p>只要是能够计算的指标，都是可以用欧式距离来计算</p>
<h4 id="只针对房间个数进行计算（单指标）"><a href="#只针对房间个数进行计算（单指标）" class="headerlink" title="只针对房间个数进行计算（单指标）"></a>只针对房间个数进行计算（单指标）</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 添加一个指标distance，用于存储房间个数的距离</span></span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line">our_acc_value = <span class="number">3</span>       <span class="comment"># 设置待预测的房间个数</span></span><br><span class="line">data[<span class="string">'distance'</span>] = np.abs(data.accommodates - our_acc_value)</span><br><span class="line">data.distance.value_counts().sort_index()   <span class="comment"># 根据结果可以看到距离为0的有461个，说明有461个样本的房间个数为3，我们从中取K个求平均值</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 洗牌与排序的操作</span></span><br><span class="line">data = data.sample(frac=<span class="number">1</span>, random_state=<span class="number">0</span>)  <span class="comment"># 为了消除数据样本间可能的关联，对数据进行一个洗牌的操作</span></span><br><span class="line">data = data.sort_values(<span class="string">'distance'</span>)         <span class="comment"># 是否有在想，既然都排序了，洗牌操作有用么？当然有用，这里排序是根据distance排序的，相同的distance的记录的顺序是可以打乱的</span></span><br><span class="line">data.price.head()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 计算前K个记录的均值</span></span><br><span class="line">data[<span class="string">'price'</span>] = data.price.str.replace(<span class="string">"\$|,"</span>, <span class="string">''</span>).astype(float)    <span class="comment"># 样本中的价格字段是字符串，且包含特殊符号</span></span><br><span class="line">mean_price = data.price.iloc[:<span class="number">5</span>].mean()</span><br><span class="line">print(mean_price)</span><br></pre></td></tr></table></figure>
<h3 id="解决问题"><a href="#解决问题" class="headerlink" title="解决问题"></a>解决问题</h3><p>通过上面的分析，我们已经知道了knn的原理，同时也能够根据某个指标来计算价格，但是计算出来后到底怎么样，我们需要有个模型评估的过程</p>
<p>这里我们将数据的75%分为训练集，25%分为测试集</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">data.drop(<span class="string">'distance'</span>, axis=<span class="number">1</span>)       <span class="comment"># 删除之前计算的distance</span></span><br><span class="line">train_df = data.copy().iloc[:<span class="number">2792</span>]</span><br><span class="line">test_df = data.copy().iloc[<span class="number">2792</span>:]</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">predict_price</span><span class="params">(new_listing_value, features_column)</span>:</span></span><br><span class="line">    temp_df = train_df</span><br><span class="line">    temp_df[<span class="string">'distance'</span>] = np.abs(data[features_column] - new_listing_value)</span><br><span class="line">    temp_df = temp_df.sort_values(<span class="string">'distance'</span>)</span><br><span class="line">    knn_5 = temp_df.price.iloc[:<span class="number">5</span>]</span><br><span class="line">    predicted_price = knn_5.mean()</span><br><span class="line">    <span class="keyword">return</span> predicted_price</span><br><span class="line"></span><br><span class="line"><span class="comment"># 计算测试集的预测值，其是根据训练集的数据计算而来</span></span><br><span class="line"><span class="comment"># 这里的apply会将每个样本的accommodates值作为predict_price的第一个参数进行计算</span></span><br><span class="line">test_df[<span class="string">'predicted_price'</span>] = test_df.accommodates.apply(predict_price, features_column=<span class="string">'accommodates'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="模型评估"><a href="#模型评估" class="headerlink" title="模型评估"></a>模型评估</h3><p>通过上面的计算，我们能够非常方便的根据每个指标计算预测价格值，这里我们看看如何进行模型评估</p>
<h4 id="RMSE（root-mean-squared-error，均方根误差）"><a href="#RMSE（root-mean-squared-error，均方根误差）" class="headerlink" title="RMSE（root mean squared error，均方根误差）"></a>RMSE（root mean squared error，均方根误差）</h4><script type="math/tex; mode=display">
RMSE = \sqrt {\frac{(actual_1 - predicted_1)^2 + (actual_2 - predicted_2)^2 + \cdots + (actual_n - predicted_n)^2}{n}}</script><p>通过公式我们可以看到，这个公式计算了真实值与预测值间的差值，表达了模型是有多么不好，数值越大越不好，计算也是非常简单</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">test_df[<span class="string">'squared_error'</span>] = (test_df[<span class="string">'predicted_price'</span>] - test_df[<span class="string">'price'</span>]) ** <span class="number">2</span></span><br><span class="line">mse = test_df[<span class="string">'squared_error'</span>].mean()</span><br><span class="line">rmse = mse ** (<span class="number">1</span>/<span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<p>我们分别计算一下不同指标的rmse值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> feature <span class="keyword">in</span> [<span class="string">'accommodates'</span>, <span class="string">'bedrooms'</span>, <span class="string">'bathrooms'</span>, <span class="string">'number_of_reviews'</span>]:</span><br><span class="line">    test_df[<span class="string">'predicted_price'</span>] = test_df[feature].apply(predict_price, features_column=feature)</span><br><span class="line">    test_df[<span class="string">'squared_error'</span>] = (test_df[<span class="string">'predicted_price'</span>] - test_df[<span class="string">'price'</span>])**<span class="number">2</span></span><br><span class="line">    rmse = test_df[<span class="string">'squared_error'</span>].mean() ** (<span class="number">1</span>/<span class="number">2</span>)</span><br><span class="line">    print(<span class="string">"RMSE for the &#123;&#125; column: &#123;&#125;"</span>.format(feature, rmse))</span><br></pre></td></tr></table></figure>
<h2 id="进阶（多变量KNN模型）"><a href="#进阶（多变量KNN模型）" class="headerlink" title="进阶（多变量KNN模型）"></a>进阶（多变量KNN模型）</h2><p>在基础篇我们试验了计算一个指标的预测价格，同时也试验了如何评估模型好坏，确实也看到了差别。但是我们有多个指标，如何将它们统一结合起来完成我们的终极预测目标呢？</p>
<h3 id="数据预处理"><a href="#数据预处理" class="headerlink" title="数据预处理"></a>数据预处理</h3><p>为什么要进行数据预处理？</p>
<p>通过欧式距离，我们知道，要计算每个指标的差异，这样的计算会受原始数据性质的影响，取值比较大的数据天生比取值小的数据距离大</p>
<p>比如有房间个数和平方面积两个指标，如果不进行预处理，面积的影响将大于房间个数，但是我们并没有这样的假设，因此我们需要消除这种问题</p>
<h4 id="标准化（standardization-或-Z-score-normalization）"><a href="#标准化（standardization-或-Z-score-normalization）" class="headerlink" title="标准化（standardization 或 Z-score normalization）"></a>标准化（standardization 或 Z-score normalization）</h4><p>让我们的数据形成一个新的分布（均值为0，标准差为1的分布）</p>
<script type="math/tex; mode=display">
z = \frac {x - \mu}{\sigma}</script><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn <span class="keyword">import</span> preprocessing</span><br><span class="line">std_scale = preprocession.StandardScaler().fit(df)</span><br></pre></td></tr></table></figure>
<h4 id="归一化（Min-Max-scaling-或-normalization）"><a href="#归一化（Min-Max-scaling-或-normalization）" class="headerlink" title="归一化（Min-Max scaling 或 normalization）"></a>归一化（Min-Max scaling 或 normalization）</h4><p>将我们所有特征的值压缩到0到1的区间上，这样做还可以抑制离群值对结果的影响</p>
<script type="math/tex; mode=display">
X_{norm} = \frac {X - X_{min}}{X_{max} - X_{min}}</script><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn <span class="keyword">import</span> preprocessing</span><br><span class="line">minmax_scale = preprocession.MinMaxScaler().fit(df)</span><br></pre></td></tr></table></figure>
<h3 id="重新处理我们的listing数据"><a href="#重新处理我们的listing数据" class="headerlink" title="重新处理我们的listing数据"></a>重新处理我们的listing数据</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">from</span> sklearn.preprocessing <span class="keyword">import</span> StandardScaler</span><br><span class="line">features = [<span class="string">'accommodates'</span>, <span class="string">'bedrooms'</span>, <span class="string">'bathrooms'</span>, <span class="string">'beds'</span>, <span class="string">'price'</span>, <span class="string">'minimum_nights'</span>, <span class="string">'maximum_nights'</span>, <span class="string">'number_of_reviews'</span>]</span><br><span class="line">data = pd.read_csv(<span class="string">'listings.csv'</span>)</span><br><span class="line">data = data[features]</span><br><span class="line">data[<span class="string">'price'</span>] = data.price.str.replace(<span class="string">"\$|,"</span>, <span class="string">""</span>).astype(float)</span><br><span class="line">data = data.dropna()        <span class="comment"># 直接删除有缺失值的记录</span></span><br><span class="line">data[features] = StandardScaler().fit_transform(data[features])</span><br><span class="line">normalized_data = data</span><br></pre></td></tr></table></figure>
<h3 id="开始多变量欧式距离计算"><a href="#开始多变量欧式距离计算" class="headerlink" title="开始多变量欧式距离计算"></a>开始多变量欧式距离计算</h3><p><strong>使用scipy计算欧式距离</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> scipy.spatial <span class="keyword">import</span> distance</span><br><span class="line">first_data = normalized_data.iloc[<span class="number">0</span>][[<span class="string">'accommodates'</span>, <span class="string">'bathrooms'</span>]]</span><br><span class="line">fifth_data = normalized_data.iloc[<span class="number">20</span>][[<span class="string">'accommodates'</span>, <span class="string">'bathrooms'</span>]]</span><br><span class="line">first_fifth_distance = distance.euclidean(first_data, fifth_data)</span><br></pre></td></tr></table></figure>
<p>多个变量参与预测价格</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">norm_train_df = normalized_data.copy().iloc[:<span class="number">2792</span>]</span><br><span class="line">norm_test_df = normalized_data.copy().iloc[<span class="number">2792</span>:]</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">predict_price_multivariate</span><span class="params">(new_listing_value, feature_columns)</span>:</span></span><br><span class="line">    temp_df = norm_train_df</span><br><span class="line">    temp_df[<span class="string">'distance'</span>] = distance.cdist(temp_df[feature_columns], [new_listing_value[feature_columns]])</span><br><span class="line">    temp_df = temp_df.sort_values(<span class="string">'distance'</span>)</span><br><span class="line">    knn_5 = temp_df.price.iloc[:<span class="number">5</span>]</span><br><span class="line">    predicted_price = knn_5.mean()</span><br><span class="line">    <span class="keyword">return</span> predicted_price</span><br><span class="line"></span><br><span class="line">cols = [<span class="string">'accommodates'</span>, <span class="string">'bathrooms'</span>]</span><br><span class="line">norm_test_df[<span class="string">'predicted_price'</span>] = norm_test_df[cols].apply(predict_price_multivariate, feature_columns=cols, axis=<span class="number">1</span>)</span><br><span class="line">norm_test_df[<span class="string">'squared_error'</span>] = (norm_test_df[<span class="string">'predicted_price'</span>] - norm_test_df[<span class="string">'price'</span>]) ** <span class="number">2</span></span><br><span class="line">rmse = norm_test_df[<span class="string">'squared_error'</span>].mean() ** (<span class="number">1</span>/<span class="number">2</span>)</span><br><span class="line">print(rmse)</span><br></pre></td></tr></table></figure>
<h3 id="使用Sklearn来完成KNN"><a href="#使用Sklearn来完成KNN" class="headerlink" title="使用Sklearn来完成KNN"></a>使用Sklearn来完成KNN</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.neighbors <span class="keyword">import</span> KNeighborsRegressor</span><br><span class="line">cols = [<span class="string">'accommodates'</span>, <span class="string">'bathrooms'</span>]            <span class="comment"># 可以选择多个指标</span></span><br><span class="line">knn = KNeighborsRegressor(n_neighbors=<span class="number">5</span>)        <span class="comment"># k的值，默认为5，可省略</span></span><br><span class="line">knn.fit(norm_train_df[cols], norm_train_df[<span class="string">'price'</span>])</span><br><span class="line">two_features_predictions = knn.predict(norm_test_df[cols])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 计算rmse</span></span><br><span class="line"><span class="keyword">from</span> sklearn.metrics <span class="keyword">import</span> mean_squared_error</span><br><span class="line">two_features_mse = mean_squared_error(norm_test_df[<span class="string">'price'</span>], two_features_predictions)</span><br><span class="line">two_features_rmse = two_features_mse ** (<span class="number">1</span>/<span class="number">2</span>)</span><br><span class="line">print(two_features_rmse)</span><br></pre></td></tr></table></figure>
<h2 id="knn优缺点"><a href="#knn优缺点" class="headerlink" title="knn优缺点"></a>knn优缺点</h2><h3 id="缺点"><a href="#缺点" class="headerlink" title="缺点"></a>缺点</h3><p>由于要计算出最小距离，因此需要与每条数据进行比对，当数据非常大的时候会非常慢</p>
<h3 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h3><p>knn不需要训练模型，直接用即可</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/09/20/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/09/20/1.html" itemprop="url">数据分析之seaborn</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-20T08:43:04+08:00">
                2018-09-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/" itemprop="url" rel="index">
                    <span itemprop="name">数据分析</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/seanborn/" itemprop="url" rel="index">
                    <span itemprop="name">seanborn</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/09/20/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/09/20/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/09/20/1.html" class="leancloud_visitors" data-flag-title="数据分析之seaborn">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="seaborn"><a href="#seaborn" class="headerlink" title="seaborn"></a>seaborn</h1><p>上一篇文章我们讲matplotlib这个库，有没有觉得非常强大呢？即便matplotlib非常强大，对用使用者来说，我们还是要注意很多细节</p>
<p>比如：线条颜色、布局、大小等，那么有没有不让我们设置这些跟实际数据无关的内容呢，答案是有的，就是seaborn</p>
<p>seaborn底层还是matplotlib，只是对其进行了一层封装，让使用者不用设置那么多参数，seaborn会提供一些模板，基本就能够满足工作需要了，让我开始看看吧</p>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>如果还没有安装，使用如下命令安装即可：<code>pip install seaborn</code></p>
<h2 id="基础使用"><a href="#基础使用" class="headerlink" title="基础使用"></a>基础使用</h2><h3 id="画风设置"><a href="#画风设置" class="headerlink" title="画风设置"></a>画风设置</h3><p>seanborn帮我们内置了一些画图的风格，我们只需要选择可选的风格就可以</p>
<ol>
<li>整体风格设置</li>
</ol>
<p>可选风格：</p>
<pre><code>* darkgrid
* whitegrid
* dark
* white
* ticks
</code></pre><p>试验一下吧，这里给出画图数据<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> seaborn <span class="keyword">as</span> sns</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> matplotlib <span class="keyword">as</span> mpl</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line">%matplotlib inline</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">sinplot</span><span class="params">(flip=<span class="number">1</span>)</span>:</span></span><br><span class="line">    x = np.linspace(<span class="number">0</span>, <span class="number">14</span>, <span class="number">100</span>)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>, <span class="number">7</span>):</span><br><span class="line">        plt.plot(x, np.sin(x + i * <span class="number">0.5</span>) * (<span class="number">7</span> - i) * flip)</span><br><span class="line"></span><br><span class="line">sinplot()   <span class="comment"># 这里看下结果</span></span><br><span class="line"></span><br><span class="line">sns.set()   <span class="comment"># 充值默认风格</span></span><br><span class="line">sinplot()   <span class="comment"># 对比一下是否有不同</span></span><br></pre></td></tr></table></figure></p>
<ol>
<li><p>使用一种风格</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sns.set_style(<span class="string">"whitegrid"</span>)</span><br><span class="line">data = np.random.normal(size=(<span class="number">20</span>, <span class="number">6</span>)) + np.arange(<span class="number">6</span>) / <span class="number">2</span></span><br><span class="line">sns.boxplot(data=data)</span><br></pre></td></tr></table></figure>
</li>
<li><p>风格调整函数despine</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用默认值，去掉上面与右面的轴线</span></span><br><span class="line">sns.despine()   </span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置轴线与图的偏移</span></span><br><span class="line">sns.despine(offset=<span class="number">10</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置是否去掉某个方向的轴线</span></span><br><span class="line">sns.despine(left=<span class="keyword">True</span>, bottom=<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>在有子图的情况下，我们可能要设置不同风格的子图</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> sns.axes_style(<span class="string">"darkgrid"</span>):</span><br><span class="line">    plt.subplot(<span class="number">211</span>)</span><br><span class="line">    sinplot()</span><br><span class="line">plt.subplot(<span class="number">212</span>)</span><br><span class="line">sinplot()</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="字体、线条大小等的设置"><a href="#字体、线条大小等的设置" class="headerlink" title="字体、线条大小等的设置"></a>字体、线条大小等的设置</h3><ol>
<li>上面的set_style函数用于设置风格，这里我们介绍set_context主要设置尺寸方面的内容</li>
</ol>
<p><strong>可选context</strong></p>
<pre><code>* paper
* talk
* poster
* notebook
</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">sns.set_context(<span class="string">"talk"</span>)</span><br><span class="line">sinplot()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们还能设置字体大小</span></span><br><span class="line">sns.set_context(<span class="string">"notebook"</span>, font_scale=<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置线条宽度</span></span><br><span class="line">sns.set_context(<span class="string">"notebook"</span>, rc=&#123;<span class="string">"lines.linewidth"</span>: <span class="number">4.5</span>&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="颜色设置"><a href="#颜色设置" class="headerlink" title="颜色设置"></a>颜色设置</h3><p>颜色在画图中非常重要，比如可以反映数据的重要程度、可以区分不同组等等，那么seaborn想到了，给我们提供了非常丰富的选择</p>
<p>调色板：</p>
<ul>
<li>颜色很重要</li>
<li>color_palette() 能传入任何Maplotlib所支持的颜色</li>
<li>color_palette() 不写参数则默认颜色</li>
<li>set_palette() 设置所有图的颜色</li>
</ul>
<ol>
<li>默认情况下，seanborn给我们提供了默认六种颜色，深色调</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">current_palette = sns.color_palette()</span><br><span class="line">sns.palplot(current_palette)        <span class="comment"># 画图过程如果不设置颜色，默认会用这六个颜色循环</span></span><br></pre></td></tr></table></figure>
<ol>
<li>如果多余6个指标，要用多余6个颜色怎么办呢，这里介绍圆形画板</li>
</ol>
<p>当有六个以上的分类要区分，最简单的方法就是在一个圆形的颜色区间茁均匀间隔的颜色（这样的色调会保持亮度和饱和度不变）<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">sns.palplot(sns.color_palette(<span class="string">"hls"</span>, <span class="number">12</span>))   <span class="comment"># 一行代码即可调出颜色</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 如何使用呢</span></span><br><span class="line">data = np.random.normal(size=(<span class="number">20</span>,<span class="number">8</span>)) + np.arange(<span class="number">8</span>) / <span class="number">2</span></span><br><span class="line">sns.boxplot(data=data, palette=sns.color_palette(<span class="string">"hls"</span>, <span class="number">8</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置亮度与饱和度</span></span><br><span class="line">sns.hls_palette(<span class="number">8</span>, l=<span class="number">0.7</span>, s=<span class="number">0.9</span>)</span><br><span class="line">data = np.random.normal(size=(<span class="number">20</span>,<span class="number">8</span>)) + np.arange(<span class="number">8</span>) / <span class="number">2</span></span><br><span class="line">sns.boxplot(data=data, palette=sns.hls_palette(<span class="number">8</span>, l=<span class="number">0.7</span>, s=<span class="number">0.7</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 总结一下这两个的区别</span></span><br><span class="line">sns.color_palette   <span class="comment"># 指定颜色模式与数量，这里可以设置hls、Paired模式 </span></span><br><span class="line"><span class="comment"># Paired用于生成一对一对深浅的数据</span></span><br><span class="line">sns.hls_palette     <span class="comment"># 对hls这种颜色模式的调节</span></span><br></pre></td></tr></table></figure></p>
<ol>
<li>除了上面介绍的palette设置颜色外，还可以用xkcd命令的颜色指定方式</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 需要长记性查询官网，看都可以有什么选择</span></span><br><span class="line">plt.plot([<span class="number">0</span>,<span class="number">1</span>], [<span class="number">0</span>,<span class="number">1</span>], sns.xkcd_rgb[<span class="string">"pale red"</span>], lw=<span class="number">3</span>)</span><br><span class="line">plt.plot([<span class="number">0</span>,<span class="number">1</span>], [<span class="number">0</span>,<span class="number">2</span>], sns.xkcd_rgb[<span class="string">"medium green"</span>], lw=<span class="number">3</span>)</span><br><span class="line">plt.plot([<span class="number">0</span>,<span class="number">1</span>], [<span class="number">0</span>,<span class="number">3</span>], sns.xkcd_rgb[<span class="string">"denim blue"</span>], lw=<span class="number">3</span>)</span><br></pre></td></tr></table></figure>
<ol>
<li><p>上面我们的所有颜色都是离散的，这里我们介绍如何使用连续的颜色，由浅入深的这种</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sns.palplot(sns.color_palette(<span class="string">"Blues"</span>))     <span class="comment"># 后面加上_r则是又深入浅</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>也可以设置色调线性变换</p>
</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sns.palplot(sns.color_palette(<span class="string">"cubehelix"</span>, <span class="number">8</span>))</span><br><span class="line"><span class="comment"># 也可以细节调节</span></span><br><span class="line">sns.palplot(sns.cubehelix_palette(<span class="number">8</span>, start=<span class="number">0.5</span>, rot=<span class="number">-0.75</span>))</span><br></pre></td></tr></table></figure>
<ol>
<li><p>指定一个颜色深浅</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">sns.palplot(sns.light_palette(<span class="string">"green"</span>))</span><br><span class="line">sns.palplot(sns.dark_palette(<span class="string">"green"</span>))</span><br><span class="line">sns.palplot(sns.dark_palette(<span class="string">"green"</span>), reverse=<span class="keyword">True</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 示例</span></span><br><span class="line">x, y = np.random.multivariate_normal([<span class="number">0</span>, <span class="number">0</span>], [[<span class="number">1</span>, <span class="number">-0.5</span>], [<span class="number">-0.5</span>, <span class="number">1</span>]], size=<span class="number">300</span>).T</span><br><span class="line">pal = sns.dark_palette(<span class="string">"green"</span>, as_cmap=<span class="keyword">True</span>)</span><br><span class="line">sns.kdeplot(x, y, cmap=pal)</span><br></pre></td></tr></table></figure>
</li>
<li><p>也可以自己设定颜色值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 了解即可，不用自己调参数</span></span><br><span class="line">sns.palplot(sns.light_palette((<span class="number">210</span>, <span class="number">90</span>, <span class="number">60</span>), input=<span class="string">"husl"</span>))</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="实践篇"><a href="#实践篇" class="headerlink" title="实践篇"></a>实践篇</h2><h3 id="单变量分析绘图"><a href="#单变量分析绘图" class="headerlink" title="单变量分析绘图"></a>单变量分析绘图</h3><p>我们通过画图来分析单个变量的分布情况</p>
<p><strong>一些初始化导入（与单变量分析无关）</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">%matplotlib inline</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">from</span> scipy <span class="keyword">import</span> stats, integrate</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> seaborn <span class="keyword">as</span> sns</span><br><span class="line">sns.set(color_codes=<span class="keyword">True</span>)</span><br><span class="line">np.random.seed(sum(map(ord, <span class="string">"distributions"</span>)))</span><br></pre></td></tr></table></figure>
<ol>
<li>我们使用直方图来分析单变量情况</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">x = np.random.normal(size=<span class="number">100</span>)</span><br><span class="line">sns.distplot(x, kde=<span class="keyword">False</span>)      <span class="comment"># kde 参数可忽略</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们也可以设置bins</span></span><br><span class="line">sns.distplot(x, bins=<span class="number">20</span>, kde=<span class="keyword">False</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们也可以画出一条趋势线</span></span><br><span class="line">sns.distplot(x, bins=<span class="number">20</span>, kde=<span class="keyword">False</span>, fit=stats.gamma)</span><br></pre></td></tr></table></figure>
<h3 id="多变量分析绘图"><a href="#多变量分析绘图" class="headerlink" title="多变量分析绘图"></a>多变量分析绘图</h3><p>单变量我们使用了直方图分析，对于多变量推荐使用散点图来分析</p>
<ol>
<li>使用散点图分析多个指标</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 指定均值与协方差</span></span><br><span class="line">mean, cov = [<span class="number">0</span>, <span class="number">1</span>], [(<span class="number">1</span>, <span class="number">.5</span>), (<span class="number">.5</span>, <span class="number">1</span>)]</span><br><span class="line">data = np.random.multivariate_normal(mean, cov, <span class="number">200</span>)</span><br><span class="line">df = pd.DataFrame(data, columns=[<span class="string">"x"</span>, <span class="string">"y"</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># seaborn提供的这个函数会同时画出两个指标的直方图情况，也会画图一个散点图</span></span><br><span class="line">sns.jointplot(x=<span class="string">"x"</span>, y=<span class="string">"y"</span>, data=df)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 当我们的数据非常多的时候，点比较集中的位置可能变成一片同色的区域不好区分，这里介绍另外一种方式，自己试验一下吧</span></span><br><span class="line">sns.jointplot(x=<span class="string">"x"</span>, y=<span class="string">"y"</span>, data=df, kind=<span class="string">"hex"</span>, color=<span class="string">"k"</span>)</span><br></pre></td></tr></table></figure>
<ol>
<li>假设我们有4个指标，我们想要看4个指标两两之间的关系，我们是不是要来个for循环一次遍历呢？</li>
</ol>
<p>不需要，seanborn帮我们提供了好用的函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用seaborn内置的数据集 iris</span></span><br><span class="line">iris = sns.load_dataset(<span class="string">"iris"</span>)</span><br><span class="line">sns.pairplot(iris)      <span class="comment"># 自己试验一下吧</span></span><br></pre></td></tr></table></figure>
<ol>
<li>关于离散值变量的绘制</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们还是拿小费的例子来看</span></span><br><span class="line">tips = sns.load_dataset(<span class="string">"tips"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 其中的day属性是一个离散值，看不同天与总金额的关系</span></span><br><span class="line">sns.stripplot(x=<span class="string">"day"</span>, y=<span class="string">"total_bill"</span>, data=tips)</span><br><span class="line"><span class="comment"># 从结果可以看到，数据密集的地方都变成一条直线了，不方便分析</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 添加jitter属性，让离散值进行左右小幅度偏移，方便查看分布情况</span></span><br><span class="line">sns.stripplot(x=<span class="string">"day"</span>, y=<span class="string">"total_bill"</span>, data=tips, jitter=<span class="keyword">True</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 还有另外一种图，更直观的表达这种情况，自己试验一下，看看结果吧</span></span><br><span class="line">sns.swarmplot(x=<span class="string">"day"</span>, y=<span class="string">"total_bill"</span>, data=tips)</span><br></pre></td></tr></table></figure>
<ol>
<li>使用hue属性，更方便的看细化指标</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 还是以tips数据集为例，查看day与total_bill的分布情况，我们可以添加hue属性来区分男女用餐情况</span></span><br><span class="line">sns.swarmplot(x=<span class="string">"day"</span>, y=<span class="string">"total_bill"</span>, hue=<span class="string">"sex"</span>, data=tips)</span><br></pre></td></tr></table></figure>
<ol>
<li>使用盒图</li>
</ol>
<p>在上一篇文章中已经介绍了盒图，这里回顾一下：</p>
<pre><code>1. IQR即统计学概念四分位距，第1/4分位与第3/4分位之间的距离
2. N=1.5IQR  如果有一个值x  x &gt; Q3 + N || x &lt; Q1 - N ， 则x为离群点
</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 还是以tips为例</span></span><br><span class="line">sns.boxplot(x=<span class="string">"day"</span>, y=<span class="string">"total_bill"</span>, hue=<span class="string">"time"</span>, data=tips)</span><br><span class="line">sns.boxplot(data=tips)  <span class="comment"># 自动寻找可以画的指标</span></span><br><span class="line">sns.boxplot(data=tips, orient=<span class="string">"h"</span>)  <span class="comment"># 改变方向，如果通过x与y指定的话，交换x与y的属性即可</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 小提琴图</span></span><br><span class="line">sns.violinplot(x=<span class="string">"total_bill"</span>, y=<span class="string">"day"</span>, hue=<span class="string">"sex"</span>, data=tips)  <span class="comment"># 我们将x与y交换了一下位置</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们还可以设置split属性，将hue指定的类别分别花在左右两边，自己试验一下吧</span></span><br><span class="line">sns.violinplot(x=<span class="string">"total_bill"</span>, y=<span class="string">"day"</span>, hue=<span class="string">"sex"</span>, data=tips, split=<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
<ol>
<li>条形图（同时绘制分类属性）</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">titanic = sns.load_dataset(<span class="string">"titanic"</span>)</span><br><span class="line">sns.barplot(x=<span class="string">"sex"</span>, y=<span class="string">"survived"</span>, hue=<span class="string">"class"</span>, data=titanic)</span><br></pre></td></tr></table></figure>
<ol>
<li>点图（描述变化情况的图）</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">titanic = sns.load_dataset(<span class="string">"titanic"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 关注不同性别的变化趋势</span></span><br><span class="line">sns.pointplot(x=<span class="string">"sex"</span>, y=<span class="string">"survived"</span>, hue=<span class="string">"class"</span>, data=titanic)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 关注不同船舱等级的变化趋势</span></span><br><span class="line">sns.pointplot(x=<span class="string">"class"</span>, y=<span class="string">"survived"</span>, hue=<span class="string">"sex"</span>, data=titanic)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们还可以美化，设置线条样式与marker样式</span></span><br><span class="line">sns.pointplot(x=<span class="string">"class"</span>, y=<span class="string">"survived"</span>, hue=<span class="string">"sex"</span>, data=titanic, palette=&#123;<span class="string">"male"</span>: <span class="string">"g"</span>, <span class="string">"female"</span>: <span class="string">"m"</span>&#125;, markers=[<span class="string">"^"</span>, <span class="string">"o"</span>], linestyles=[<span class="string">"-"</span>, <span class="string">"--"</span>])</span><br><span class="line"><span class="comment"># 这里要特别注意，如果hue指定的是class属性，那么palette参数的字典里的key就应该是船舱等级类别，markers与linestyles的字典里元素也应该是对应类别的数量</span></span><br></pre></td></tr></table></figure>
<ol>
<li>组合</li>
</ol>
<p>我们能够将多个类型的图放在一起</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sns.violinplot(x=<span class="string">"day"</span>, y=<span class="string">"total_bill"</span>, data=tips, inner=<span class="keyword">None</span>)</span><br><span class="line">sns.swarmplot(x=<span class="string">"day"</span>, y=<span class="string">"total_bill"</span>, data=tips, color=<span class="string">"w"</span>, alpha=<span class="number">.5</span>)</span><br></pre></td></tr></table></figure>
<h3 id="回归分析绘图"><a href="#回归分析绘图" class="headerlink" title="回归分析绘图"></a>回归分析绘图</h3><p>seaborn帮我们提供了regplot与implot两个函数来画回归图，推荐使用regplot，相对比较简单，如果感兴趣可以自行查看implot</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们还是用seanborn内置的数据集tips，这是一个饭店顾客小费的情况</span></span><br><span class="line">tips = sns.load_dataset(<span class="string">"tips"</span>)</span><br><span class="line">tips.head()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 描述总花费与小费的回归情况</span></span><br><span class="line">sns.regplot(x=<span class="string">"total_bill"</span>, y=<span class="string">"tip"</span>, data=tips) <span class="comment"># 使用上还是非常简单的，分别指定对应指标的列名即可</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 描述吃饭人数与小费的回归情况</span></span><br><span class="line">sns.regplot(x=<span class="string">"size"</span>, y=<span class="string">"tip"</span>, data=tips)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 由于size是离散值，不适合做回归分析，这时我们可以添加一些抖动，就是将离散值稍微随机变动一下</span></span><br><span class="line">sns.regplot(x=<span class="string">"size"</span>, y=<span class="string">"tip"</span>, data=tips, x_jitter=<span class="number">.05</span>)</span><br></pre></td></tr></table></figure>
<h2 id="高级篇"><a href="#高级篇" class="headerlink" title="高级篇"></a>高级篇</h2><h3 id="factorplot"><a href="#factorplot" class="headerlink" title="factorplot"></a>factorplot</h3><p>万能画图函数factorplot</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 不指定类别，默认是点图</span></span><br><span class="line">sns.factorplot(x=<span class="string">"class"</span>, y=<span class="string">"survived"</span>, hue=<span class="string">"sex"</span>, data=titanic)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定type使用直方图</span></span><br><span class="line">sns.factorplot(x=<span class="string">"class"</span>, y=<span class="string">"survived"</span>, hue=<span class="string">"sex"</span>, data=titanic, kind=<span class="string">"bar"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定col，根据条件绘制多个图</span></span><br><span class="line">sns.factorplot(x=<span class="string">"class"</span>, y=<span class="string">"survived"</span>, hue=<span class="string">"sex"</span>, col=<span class="string">"alone"</span> ,data=titanic, kind=<span class="string">"bar"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># type参数可选值</span></span><br><span class="line">point       点图，默认</span><br><span class="line">bar         柱形图</span><br><span class="line">count       频次</span><br><span class="line">box         箱体</span><br><span class="line">violin      小提琴</span><br><span class="line">strip       散点</span><br><span class="line">swarm       分散点（树）</span><br></pre></td></tr></table></figure>
<h3 id="FacetGrid"><a href="#FacetGrid" class="headerlink" title="FacetGrid"></a>FacetGrid</h3><p>FacetGrid用于根据不同指标画多个图</p>
<ol>
<li><p>基础</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line">tips = sns.load_dataset(<span class="string">"tips"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据time指标画图，time总共只有两个值，因此会画出两个图</span></span><br><span class="line">g = sns.FacetGrid(tips, col=<span class="string">"time"</span>)</span><br><span class="line">g.map(plt.hist, <span class="string">"tip"</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>同样可以设置hue</p>
</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">g = sns.FacetGrid(tips, col=<span class="string">"sex"</span>, hue=<span class="string">"smoker"</span>)</span><br><span class="line">g.map(plt.scatter, <span class="string">"total_bill"</span>, <span class="string">"tip"</span>, alpha=<span class="number">.7</span>)</span><br><span class="line">g.add_legend()  <span class="comment"># hue指定的指标需要标识出来</span></span><br></pre></td></tr></table></figure>
<ol>
<li>上面的图形，我们按性别画不同的图，那如果出了性别还有加一个是否吸烟，应该如何做呢</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 设置行为smoker指标，列为time指标</span></span><br><span class="line">g = sns.FacetGrid(tips, row=<span class="string">"smoker"</span>, col=<span class="string">"time"</span>, margin_titles=<span class="keyword">True</span>)</span><br><span class="line"><span class="comment"># color指定颜色深浅，fit_reg用于指定是否画回归的线</span></span><br><span class="line">g.map(sns.regplot, <span class="string">"size"</span>, <span class="string">"total_bill"</span>, color=<span class="string">".5"</span>, fit_reg=<span class="keyword">False</span>, x_jitter=<span class="number">.1</span>)</span><br></pre></td></tr></table></figure>
<ol>
<li>我们也可以设置图形大小与长宽比</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">g = sns.FacetGrid(tips, col=<span class="string">"day"</span>, size=<span class="number">4</span>, aspect=<span class="number">.5</span>)</span><br><span class="line">g.map(sns.barplot, <span class="string">"sex"</span>, <span class="string">"total_bill"</span>)</span><br></pre></td></tr></table></figure>
<ol>
<li>关于多个图的顺序</li>
</ol>
<p>上面的例子我们通过指定col来表明要画的多个图的区分指标，这时的顺序都是默认的，我们也可以通过row_order来调整顺序</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以tips数据集为例，找出day这个属性可选的值</span></span><br><span class="line">ordered_days = tips.day.value_counts().index</span><br><span class="line">print(ordered_days)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置一个顺序</span></span><br><span class="line"><span class="keyword">from</span> pandas <span class="keyword">import</span> Categorical</span><br><span class="line">ordered_days = Categorical([<span class="string">'Thur'</span>, <span class="string">'Fri'</span>, <span class="string">'Sat'</span>, <span class="string">'Sun'</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 画图</span></span><br><span class="line">g = sns.FacetGrid(tips, row=<span class="string">"day"</span>, row_order=ordered_days, size=<span class="number">1.7</span>, aspect=<span class="number">4</span>)</span><br><span class="line">g.map(sns.boxplot, <span class="string">"total_bill"</span>)</span><br></pre></td></tr></table></figure>
<ol>
<li>其他属性设置（颜色、线宽等）</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">pal = dict(Lunch=<span class="string">"seagreen"</span>, Dinner=<span class="string">"gray"</span>)</span><br><span class="line"><span class="comment"># 通过palette设置颜色，还可以通过hue_kws参数设置通用的属性，比如marker   hue_kws=&#123;"marker": ["^", "v"]&#125;</span></span><br><span class="line">g = sns.FacetGrid(tips, hue=<span class="string">"time"</span>, palette=pal, size=<span class="number">5</span>)</span><br><span class="line"><span class="comment"># s设置点的大小，alpha设置透明度，linewidth=0.7，linewidth=0.5，edgecolor设置边缘颜色</span></span><br><span class="line">g.map(plt.scatter, <span class="string">"total_bill"</span>, <span class="string">"tip"</span>, s=<span class="number">50</span>, alpha=<span class="number">.7</span>, linewidth=<span class="number">.5</span>, edgecolor=<span class="string">"white"</span>)</span><br><span class="line">g.add_legend()</span><br></pre></td></tr></table></figure>
<ol>
<li>设置轴与ticks等属性</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> sns.axes_style(<span class="string">"white"</span>):</span><br><span class="line">    g = sns.FacetGrid(tips, row=<span class="string">"sex"</span>, col=<span class="string">"smoker"</span>, margin_titles=<span class="keyword">True</span>, size=<span class="number">2.5</span>)</span><br><span class="line">g.map(plt.scatter, <span class="string">"total_bill"</span>, <span class="string">"tip"</span>, color=<span class="string">"#334488"</span>, edgecolor=<span class="string">"white"</span>, lw=<span class="number">.5</span>)</span><br><span class="line"><span class="comment"># 设置轴的labels</span></span><br><span class="line">g.set_axis_labels(<span class="string">"Total bill (US Dollars) "</span>, <span class="string">"Tip"</span>)</span><br><span class="line"><span class="comment"># 设置ticks</span></span><br><span class="line">g.set(xticks=[<span class="number">10</span>, <span class="number">30</span>, <span class="number">50</span>], yticks=[<span class="number">2</span>, <span class="number">6</span>, <span class="number">10</span>])</span><br><span class="line"><span class="comment"># 设置子图间的间距</span></span><br><span class="line">g.fig.subplots_adjust(wspace=<span class="number">.02</span>, hspace=<span class="number">.02</span>)</span><br></pre></td></tr></table></figure>
<h3 id="PairGrid（对图）"><a href="#PairGrid（对图）" class="headerlink" title="PairGrid（对图）"></a>PairGrid（对图）</h3><p>与 sns.pairplot 类似</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">iris = sns.load_dataset(<span class="string">"iris"</span>)</span><br><span class="line">g = sns.PairGrid(iris)          <span class="comment"># 我们还可以设置hue参数，与之前介绍的一样，分类画图 hue="species"</span></span><br><span class="line">g.map(plt.scatter)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 分别设置对角线与非对角线画的图类型</span></span><br><span class="line">g.map_diag(plt.hist)            <span class="comment"># 对角线画直方图</span></span><br><span class="line">g.map_offdiag(plt.scatter)      <span class="comment"># 非对角线画散点图</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果不指定要关注的指标，则会将所有可以画的指标全都画出来，我们可以通过 vars变量来设置要关注的指标</span></span><br><span class="line">g = sns.PairGrid(iris, vars=[<span class="string">"sepal_length"</span>, <span class="string">"sepal_width"</span>], hue=<span class="string">"species"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们也可以设置渐变色</span></span><br><span class="line">g = sns.PairGrid(iris, hue=<span class="string">"species"</span>, palette=<span class="string">"GnBu_d"</span>)</span><br></pre></td></tr></table></figure>
<h3 id="热度图"><a href="#热度图" class="headerlink" title="热度图"></a>热度图</h3><p>用于描述值的变化情况，看一下特征与特征间的相关程度</p>
<ol>
<li><p>一个基础的例子</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 随机构造一个3x3的矩阵，用热度图表示</span></span><br><span class="line">uniform_data = np.random.rand(<span class="number">3</span>,<span class="number">3</span>)</span><br><span class="line">heatmap = sns.heatmap(uniform_data)</span><br><span class="line"><span class="comment"># 非常直观可以看到最大最小值的坐标</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们还可以设置一个最大最小值，小于最小值的所有点都是一个颜色，所有大于最大值的点又是另外一个颜色</span></span><br><span class="line">heatmap = sns.heatmap(uniform_data, vmin=<span class="number">0.2</span>, vmax=<span class="number">0.5</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们也可以设置一个中心值，在中心值的两次有比较清晰的区分</span></span><br><span class="line">normal_data = np.random.randn(<span class="number">3</span>,<span class="number">3</span>)</span><br><span class="line">ax = sns.heatmap(normal_data, center=<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>以seanborn自带的 flights 数据集进行试验</p>
</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">flights_data = sns.load_dataset(<span class="string">"flights"</span>)</span><br><span class="line">flights.head()  <span class="comment"># 看看内容是啥</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们使用pivot转变一下数据集</span></span><br><span class="line">flights = flights_data.pivot(<span class="string">"year"</span>, <span class="string">"month"</span>, <span class="string">"passengers"</span>)</span><br><span class="line">flights.head()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 画热度图</span></span><br><span class="line">sns.heatmap(flights)    <span class="comment"># 可以看到变化情况</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们可以将对应的值也添加进图形里</span></span><br><span class="line">sns.heatmap(flights, annot=<span class="keyword">True</span>, fmt=<span class="string">"d"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们也可以修改样式，修改线条宽度</span></span><br><span class="line">sns.heatmap(flights, linewidths=<span class="number">.5</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置调色板</span></span><br><span class="line">sns.heatmap(flights, linewidths=<span class="number">.5</span>, cmap=<span class="string">"YlGnBu"</span>)</span><br></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/09/18/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/09/18/1.html" itemprop="url">数据分析之matplotlib</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-18T09:25:47+08:00">
                2018-09-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/" itemprop="url" rel="index">
                    <span itemprop="name">数据分析</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/基础工具/" itemprop="url" rel="index">
                    <span itemprop="name">基础工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/09/18/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/09/18/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/09/18/1.html" class="leancloud_visitors" data-flag-title="数据分析之matplotlib">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="matplotlib"><a href="#matplotlib" class="headerlink" title="matplotlib"></a>matplotlib</h1><p>机器学习中一个非常重要的底层可视化库，可视化库很多，但是基本都是以matplotlib为基础</p>
<h2 id="环境"><a href="#环境" class="headerlink" title="环境"></a>环境</h2><ol>
<li><p>python command client</p>
<p>需要调用<code>show</code>方法才能显示图形</p>
</li>
<li><p>jupyter</p>
<p> 使用魔法指令<code>%matplotlib inline</code>，便可以不用调用<code>show</code>方法</p>
</li>
</ol>
<p>本篇文章为了代码展示方便，使用<code>python command client</code>的方式</p>
<h2 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h2><ol>
<li><p>来个最基础的</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 一个简单的折线图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>], [<span class="number">1</span>,<span class="number">4</span>,<span class="number">9</span>,<span class="number">16</span>,<span class="number">25</span>])</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x11e783550</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()   </span><br><span class="line"><span class="comment"># x、y的取值返回，matplotlib会自动帮我们适应</span></span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/1.png">
</li>
<li><p>让我们在这个简单的图上添加一些标签，指明x、y轴的意义，添加标题、文字</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>], [<span class="number">1</span>,<span class="number">4</span>,<span class="number">9</span>,<span class="number">16</span>,<span class="number">25</span>])</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x1202cad68</span>&gt;]</span><br><span class="line"><span class="comment"># 设置xlabel、ylabel、title等都是可以使用fontsize参数指定字体大小的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.xlabel(<span class="string">'xlabel'</span>, fontsize=<span class="number">16</span>)</span><br><span class="line">Text(<span class="number">0.5</span>,<span class="number">0</span>,<span class="string">'xlabel'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.ylabel(<span class="string">'ylabel'</span>)</span><br><span class="line">Text(<span class="number">0</span>,<span class="number">0.5</span>,<span class="string">'ylabel'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.title(<span class="string">'jackstraw: -----'</span>)</span><br><span class="line">Text(<span class="number">0.5</span>,<span class="number">1</span>,<span class="string">'jackstraw: -----'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/2.png" title="指定各种表识">
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>], [<span class="number">1</span>,<span class="number">4</span>,<span class="number">9</span>,<span class="number">16</span>,<span class="number">25</span>])</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x11e90c668</span>&gt;]</span><br><span class="line"><span class="comment"># 添加网格，让我们更清楚得看到数据情况</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.grid(<span class="keyword">True</span>)</span><br><span class="line"><span class="comment"># 在坐标（3.0, 10）的位置添加一段文字</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.text(<span class="number">3.0</span>, <span class="number">10</span>, <span class="string">"i am text"</span>)</span><br><span class="line">Text(<span class="number">3</span>,<span class="number">10</span>,<span class="string">'i am text'</span>)</span><br><span class="line"><span class="comment"># 添加注释，区别于添加文字，我们可以指定箭头</span></span><br><span class="line"><span class="comment"># xy: 箭头的坐标</span></span><br><span class="line"><span class="comment"># xytext: 文字的坐标</span></span><br><span class="line"><span class="comment"># arrowprops: 箭头的属性</span></span><br><span class="line"><span class="comment"># arrowprops(width): 线条宽度</span></span><br><span class="line"><span class="comment"># arrowprops(headlength): 箭头长度</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.annotate(<span class="string">'i am annotate'</span>, xy=(<span class="number">2.0</span>,<span class="number">4</span>), xytext=(<span class="number">2.5</span>,<span class="number">6</span>), arrowprops=dict(facecolor=<span class="string">'black'</span>,shrink=<span class="number">2</span>))</span><br><span class="line">Text(<span class="number">2.5</span>,<span class="number">6</span>,<span class="string">'i am annotate'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/3.png" title="添加说明性文字">
</li>
<li><p>让我们改变一下线条的样式，比如虚线、颜色等</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 这里的线条格式省略了 linestyle 参数名，我们同样可以指定颜色</span></span><br><span class="line"><span class="comment"># plt.plot([1,2,3,4,5], [1,4,9,16,25], '--', color='r')</span></span><br><span class="line"><span class="comment"># plt.plot([1,2,3,4,5], [1,4,9,16,25], 'r--')</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>], [<span class="number">1</span>,<span class="number">4</span>,<span class="number">9</span>,<span class="number">16</span>,<span class="number">25</span>], <span class="string">'--'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x11e99ccf8</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.xlabel(<span class="string">'xlabel'</span>, fontsize=<span class="number">16</span>)</span><br><span class="line">Text(<span class="number">0.5</span>,<span class="number">0</span>,<span class="string">'xlabel'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.ylabel(<span class="string">'ylabel'</span>, fontsize=<span class="number">16</span>)</span><br><span class="line">Text(<span class="number">0</span>,<span class="number">0.5</span>,<span class="string">'ylabel'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/4.png" title="设置线条的样式">
</li>
<li><p>设置线条风格以及关键点的风格</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.linspace(<span class="number">-10</span>, <span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.sin(x)</span><br><span class="line"><span class="comment"># linewidth: 指定线条的宽度</span></span><br><span class="line"><span class="comment"># linestyle: 指定线条风格</span></span><br><span class="line"><span class="comment"># marker: 指定关键点风格</span></span><br><span class="line"><span class="comment"># markerfacecolor: 指定关键点颜色</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(x, y, linewidth=<span class="number">2.0</span>, linestyle=<span class="string">':'</span>, marker=<span class="string">'o'</span>, markerfacecolor=<span class="string">'r'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x113fa24e0</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br><span class="line"><span class="comment"># 除了上面这种直接在plt对象上操作外，我们也可以操作plot函数返回的对象</span></span><br><span class="line"><span class="comment"># line = plt.plot(x,y)</span></span><br><span class="line"><span class="comment"># plt.setp(line, color='r', linewidth=2.0, alpha=0.5, marker='o', markerfacecolor='b')</span></span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/5.png" title="关键点样式">    
</li>
<li><p>控制图形与边缘的间距</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.arange(<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y1 = np.random.randint(<span class="number">10</span>, <span class="number">20</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y2 = np.random.randint(<span class="number">10</span>, <span class="number">20</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.barh(x, y1, color=<span class="string">'g'</span>, alpha=<span class="number">0.5</span>)</span><br><span class="line">&lt;BarContainer object of <span class="number">5</span> artists&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.barh(x, -y2, color=<span class="string">'r'</span>, alpha=<span class="number">0.5</span>)</span><br><span class="line">&lt;BarContainer object of <span class="number">5</span> artists&gt;</span><br><span class="line"><span class="comment"># 设置x轴的最小最大值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.xlim(-max(y2) - <span class="number">5</span>, max(y1) + <span class="number">5</span>)</span><br><span class="line">(<span class="number">-23</span>, <span class="number">23</span>)</span><br><span class="line"><span class="comment"># 设置y轴的最小最大值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.ylim(min(x) - <span class="number">1</span>, max(x) + <span class="number">2</span>)</span><br><span class="line">(<span class="number">-1</span>, <span class="number">6</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/6.png" title="边缘距离"> 
</li>
</ol>
<h2 id="进阶"><a href="#进阶" class="headerlink" title="进阶"></a>进阶</h2><p>通过基础的学习，我相信您已经会画一些线条图了，也能设置这些图的一些属性，现在进入进阶篇，让您更加得心应手的画图</p>
<h3 id="子图"><a href="#子图" class="headerlink" title="子图"></a>子图</h3><ol>
<li><p>一张图里画多个图形</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = np.arange(<span class="number">0</span>,<span class="number">10</span>,<span class="number">0.5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(data, data, <span class="string">'r--'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x1194d90f0</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(data, data ** <span class="number">2</span>, <span class="string">'bs'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x119418fd0</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(data, data ** <span class="number">3</span>, <span class="string">'go'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x115a8fe10</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br><span class="line"><span class="comment"># 也可以将各个数据放在一个函数里</span></span><br><span class="line"><span class="comment"># plt.plot(data, data, 'r--', </span></span><br><span class="line"><span class="comment">#          data, data ** 2, 'bs',</span></span><br><span class="line"><span class="comment">#          data, data ** 3, 'go')</span></span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/7.png" title="多线条"> 
</li>
<li><p>画多个子图（方式一）</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.linspace(<span class="number">-10</span>, <span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.sin(x)</span><br><span class="line"><span class="comment"># 声明我们要画一个什么排列的图，这里表示两行一列的第一个图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.subplot(<span class="number">211</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x1194e3780</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(x, y, color=<span class="string">'r'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x114fb9320</span>&gt;]</span><br><span class="line"><span class="comment"># 声明我们要画一个什么排列的图，这里表示两行一列的第二个图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.subplot(<span class="number">212</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x114fb98d0</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(x, y, color=<span class="string">'b'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x114fe2ef0</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/8.png" title="子图一"> 
</li>
<li><p>画多个子图（方式二）</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 除了上面这种画子图的方式，还有一种常见的方式</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, axes = plt.subplots(ncols=<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>v_bars = axes[<span class="number">0</span>].scatter(x, y, color=<span class="string">'r'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>h_bars = axes[<span class="number">1</span>].barh(x, y, color=<span class="string">'b'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/9.png" title="子图二"> 
</li>
</ol>
<h3 id="风格设置"><a href="#风格设置" class="headerlink" title="风格设置"></a>风格设置</h3><p>每个图形如果都需要我们自己去设置样式，是一件比较费时的事情，因此matplot帮我们内置了一些风格样式，我们可以直接拿来用，如果对图形风格要求不高，则可以使用</p>
<ol>
<li><p>通过<code>plt.style.available</code>，我们能看到matplot帮我们内置了很多风格</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">seaborn-dark</span><br><span class="line">seaborn-darkgrid</span><br><span class="line">seaborn-ticks</span><br><span class="line">fivethirtyeight</span><br><span class="line">seaborn-whitegrid</span><br><span class="line">classic</span><br><span class="line">_classic_test</span><br><span class="line">fast</span><br><span class="line">seaborn-talk</span><br><span class="line">seaborn-dark-palette</span><br><span class="line">seaborn-bright</span><br><span class="line">seaborn-pastel</span><br><span class="line">grayscale</span><br><span class="line">seaborn-notebook</span><br><span class="line">ggplot</span><br><span class="line">seaborn-colorblind</span><br><span class="line">seaborn-muted</span><br><span class="line">seaborn</span><br><span class="line">Solarize_Light2</span><br><span class="line">seaborn-paper</span><br><span class="line">bmh</span><br><span class="line">tableau-colorblind10</span><br><span class="line">seaborn-white</span><br><span class="line">dark_background</span><br><span class="line">seaborn-poster</span><br><span class="line">seaborn-deep</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用系统风格</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.linspace(<span class="number">-10</span>, <span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.sin(x)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(x, y)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x116f8b780</span>&gt;]</span><br><span class="line"><span class="comment"># plt.show()</span></span><br><span class="line"><span class="comment"># 指定要使用的样式，后续所有都图形都将使用新的样式风格</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.style.use(<span class="string">'dark_background'</span>)</span><br><span class="line"><span class="comment"># 对比一下调用新风格前后的差别</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(x, y)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x116f8b780</span>&gt;]</span><br><span class="line"><span class="comment"># plt.show()</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>一个特别的风格</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.linspace(<span class="number">-10</span>, <span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.sin(x)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.xkcd()</span><br><span class="line">&lt;matplotlib.rc_context object at <span class="number">0x11787c4e0</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.plot(x, y)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x1178c8320</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/10.png" title="一个特别的风格"> 
</li>
</ol>
<h3 id="不同类型的图"><a href="#不同类型的图" class="headerlink" title="不同类型的图"></a>不同类型的图</h3><h4 id="条形图"><a href="#条形图" class="headerlink" title="条形图"></a>条形图</h4><ol>
<li><p>基本条形图</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.seed(<span class="number">0</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.arange(<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.random.randint(<span class="number">-5</span>,<span class="number">5</span>,<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, axes = plt.subplots(ncols=<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>v_bars = axes[<span class="number">0</span>].bar(x, y, color=<span class="string">'r'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>h_bars = axes[<span class="number">1</span>].barh(x, y, color=<span class="string">'b'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/11.png" title="一个简单的条形图">
</li>
<li><p>对于条形图，一般有一些分界线，比如正负值的分界点0，我们现在要添加一条线来加强这个分界线的区分</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在第一个例子的基础上，添加两个调用</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, axes = plt.subplots(ncols=<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.arange(<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.random.randint(<span class="number">-5</span>,<span class="number">5</span>,<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, axes = plt.subplots(ncols=<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>v_bars = axes[<span class="number">0</span>].bar(x, y, color=<span class="string">'r'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>h_bars = axes[<span class="number">1</span>].barh(x, y, color=<span class="string">'b'</span>)</span><br><span class="line"><span class="comment"># 第0个图是竖着的bar图，因此线条应该是横向的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>axes[<span class="number">0</span>].axhline(<span class="number">0</span>, color=<span class="string">'grey'</span>, linewidth=<span class="number">2</span>)   </span><br><span class="line">&lt;matplotlib.lines.Line2D object at <span class="number">0x11afc49e8</span>&gt;</span><br><span class="line"><span class="comment"># 第1个图是横着的bar图，因此线条应该是竖着的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>axes[<span class="number">1</span>].axvline(<span class="number">0</span>, color=<span class="string">'grey'</span>, linewidth=<span class="number">2</span>)   </span><br><span class="line">&lt;matplotlib.lines.Line2D object at <span class="number">0x11afe3278</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/12.png" title="带分界线的条形图">
</li>
<li><p>根据条形图对应单位高度，设置不同颜色</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.seed(<span class="number">0</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.arange(<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.random.randint(<span class="number">-5</span>,<span class="number">5</span>,<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, ax = plt.subplots()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>v_bars = ax.bar(x, y, color=<span class="string">'lightblue'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> bar <span class="keyword">in</span> v_bars:</span><br><span class="line"><span class="meta">... </span>    print(bar)</span><br><span class="line">...</span><br><span class="line">Rectangle(xy=(<span class="number">-0.4</span>, <span class="number">0</span>), width=<span class="number">0.8</span>, height=<span class="number">0</span>, angle=<span class="number">0</span>)</span><br><span class="line">Rectangle(xy=(<span class="number">0.6</span>, <span class="number">0</span>), width=<span class="number">0.8</span>, height=<span class="number">-5</span>, angle=<span class="number">0</span>)</span><br><span class="line">Rectangle(xy=(<span class="number">1.6</span>, <span class="number">0</span>), width=<span class="number">0.8</span>, height=<span class="number">-2</span>, angle=<span class="number">0</span>)</span><br><span class="line">Rectangle(xy=(<span class="number">2.6</span>, <span class="number">0</span>), width=<span class="number">0.8</span>, height=<span class="number">-2</span>, angle=<span class="number">0</span>)</span><br><span class="line">Rectangle(xy=(<span class="number">3.6</span>, <span class="number">0</span>), width=<span class="number">0.8</span>, height=<span class="number">2</span>, angle=<span class="number">0</span>)</span><br><span class="line"><span class="comment"># 可以看到这个v_bars对象就表示条形图的柱子的集合，我们调整每一个柱子的样式</span></span><br><span class="line"><span class="comment"># 比如下面，我们将值小于零的柱子进行特别设置</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> bar <span class="keyword">in</span> v_bars:</span><br><span class="line"><span class="meta">... </span>    <span class="keyword">if</span> bar.get_height() &lt; <span class="number">0</span>:</span><br><span class="line"><span class="meta">... </span>            bar.set(edgecolor=<span class="string">'darkred'</span>, color=<span class="string">'green'</span>, linewidth=<span class="number">3</span>)</span><br><span class="line">...</span><br><span class="line">[<span class="keyword">None</span>, <span class="keyword">None</span>, <span class="keyword">None</span>]</span><br><span class="line">[<span class="keyword">None</span>, <span class="keyword">None</span>, <span class="keyword">None</span>]</span><br><span class="line">[<span class="keyword">None</span>, <span class="keyword">None</span>, <span class="keyword">None</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/13.png" title="带分界线的条形图">
</li>
<li><p>（非条形图）填充图</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.random.randn(<span class="number">100</span>).cumsum()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.linspace(<span class="number">0</span>, <span class="number">9</span>, <span class="number">100</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, ax = plt.subplots()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ax.fill_between(x, y, color=<span class="string">'lightblue'</span>)</span><br><span class="line">&lt;matplotlib.collections.PolyCollection object at <span class="number">0x11517a470</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/14.png" title="填充">
</li>
<li><p>（非条形图）复杂一点的填充图</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 再举一个复杂点的填充的例子，好好理解一下，自己删除些参数再看看变化，比如下面这个fill_between函数有两个y，与上面的例子又有何区别</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.linspace(<span class="number">0</span>, <span class="number">10</span>, <span class="number">200</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y1 = <span class="number">2</span> * x + <span class="number">1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y2 = <span class="number">3</span> * x + <span class="number">1.2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y_mean = <span class="number">0.5</span> * x * np.cos(<span class="number">2</span> * x) + <span class="number">2.5</span> * x + <span class="number">1.1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, ax = plt.subplots()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ax.fill_between(x, y1, y2, color=<span class="string">'red'</span>)</span><br><span class="line">&lt;matplotlib.collections.PolyCollection object at <span class="number">0x1151cd978</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ax.plot(x, y1, color=<span class="string">'r'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x114f205c0</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ax.plot(x, y_mean, color=<span class="string">'black'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x1151cdcf8</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ax.plot(x, y2, color=<span class="string">'r'</span>)</span><br><span class="line">[&lt;matplotlib.lines.Line2D object at <span class="number">0x1151cd908</span>&gt;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/15.png" title="复杂填充">
</li>
<li><p>使用误差棒</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.arange(<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.random.randint(<span class="number">2</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>err = [<span class="number">0.1</span>, <span class="number">0.2</span>, <span class="number">0.3</span>, <span class="number">0.4</span>, <span class="number">0.5</span>]</span><br><span class="line"><span class="comment"># 通过yerr指定误差棒数组</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.bar(x, y, yerr=err, alpha=<span class="number">0.3</span>)</span><br><span class="line">&lt;BarContainer object of <span class="number">5</span> artists&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.ylabel(<span class="string">'variable y'</span>)</span><br><span class="line">Text(<span class="number">0</span>,<span class="number">0.5</span>,<span class="string">'variable y'</span>)</span><br><span class="line"><span class="comment"># 这里在每个x的坐标位置添加对应的标签</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.xticks(x, [<span class="string">'label1'</span>, <span class="string">'label2'</span>, <span class="string">'label3'</span>, <span class="string">'label4'</span>, <span class="string">'label5'</span>])</span><br><span class="line">([&lt;matplotlib.axis.XTick object at <span class="number">0x115213f60</span>&gt;, &lt;matplotlib.axis.XTick object at <span class="number">0x115213898</span>&gt;, &lt;m</span><br><span class="line">atplotlib.axis.XTick object at <span class="number">0x1152135f8</span>&gt;, &lt;matplotlib.axis.XTick object at <span class="number">0x11526a748</span>&gt;, &lt;matpl</span><br><span class="line">otlib.axis.XTick object at <span class="number">0x11526ac18</span>&gt;], &lt;a list of <span class="number">5</span> Text xticklabel objects&gt;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/16.png" title="复杂填充">
</li>
<li><p>画一个背靠背的对比条形图</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.arange(<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y1 = np.random.randint(<span class="number">10</span>, <span class="number">20</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y2 = np.random.randint(<span class="number">10</span>, <span class="number">20</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.barh(x, y1, color=<span class="string">'g'</span>, alpha=<span class="number">0.5</span>)</span><br><span class="line">&lt;BarContainer object of <span class="number">5</span> artists&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.barh(x, -y2, color=<span class="string">'r'</span>, alpha=<span class="number">0.5</span>)</span><br><span class="line">&lt;BarContainer object of <span class="number">5</span> artists&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.xlim(-max(y2) - <span class="number">5</span>, max(y1) + <span class="number">5</span>)</span><br><span class="line">(<span class="number">-23</span>, <span class="number">24</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.ylim(min(x) - <span class="number">1</span>, max(x) + <span class="number">2</span>)</span><br><span class="line">(<span class="number">-1</span>, <span class="number">6</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/17.png" title="背靠背条形图">
</li>
<li><p>我们经常看到一张图里分了多个组，可以很直观得看到不同组的区别，试验一下结果就知道我说得是什么意思啦</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>green_data = np.random.randint(<span class="number">1</span>,<span class="number">10</span>,<span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>blue_data = np.random.randint(<span class="number">1</span>,<span class="number">10</span>,<span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>red_data = np.random.randint(<span class="number">1</span>,<span class="number">10</span>,<span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pos = np.arange(<span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>width = <span class="number">0.2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, ax = plt.subplots(figsize=(<span class="number">8</span>, <span class="number">6</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.bar(pos, green_data, width, alpha=<span class="number">0.5</span>, color=<span class="string">'g'</span>)</span><br><span class="line">&lt;BarContainer object of <span class="number">3</span> artists&gt;</span><br><span class="line"><span class="comment"># x的坐标需要依次往后挪动一定距离</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.bar([p + width <span class="keyword">for</span> p <span class="keyword">in</span> pos], blue_data, width, alpha=<span class="number">0.5</span>, color=<span class="string">'b'</span>)</span><br><span class="line">&lt;BarContainer object of <span class="number">3</span> artists&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.bar([p + width * <span class="number">2</span> <span class="keyword">for</span> p <span class="keyword">in</span> pos], red_data, width, alpha=<span class="number">0.5</span>, color=<span class="string">'r'</span>)</span><br><span class="line">&lt;BarContainer object of <span class="number">3</span> artists&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig.show()</span><br><span class="line"><span class="comment"># 自己试验一下设置xlim与ylim，让结果更好看些</span></span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/18.png" title="分组">
</li>
<li><p>在上面例子的基础上，我想在每个bar上都标注上对应的值</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>green_data = np.random.randint(<span class="number">1</span>,<span class="number">10</span>,<span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>blue_data = np.random.randint(<span class="number">1</span>,<span class="number">10</span>,<span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pos = np.arange(<span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>red_data = np.random.randint(<span class="number">1</span>,<span class="number">10</span>,<span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>width = <span class="number">0.2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig, ax = plt.subplots(figsize=(<span class="number">8</span>, <span class="number">6</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>green_bars = plt.bar(pos, green_data, width, alpha=<span class="number">0.5</span>, color=<span class="string">'g'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> bar <span class="keyword">in</span> green_bars:</span><br><span class="line"><span class="meta">... </span>    height = bar.get_height()</span><br><span class="line"><span class="meta">... </span>    plt.text(bar.get_x()+width/<span class="number">2</span>, height*(<span class="number">1.02</span>), height)</span><br><span class="line">...</span><br><span class="line">Text(<span class="number">0</span>,<span class="number">2.04</span>,<span class="string">'2'</span>)</span><br><span class="line">Text(<span class="number">1</span>,<span class="number">4.08</span>,<span class="string">'4'</span>)</span><br><span class="line">Text(<span class="number">2</span>,<span class="number">9.18</span>,<span class="string">'9'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>blue_bars = plt.bar([p + width <span class="keyword">for</span> p <span class="keyword">in</span> pos], blue_data, width, alpha=<span class="number">0.5</span>, color=<span class="string">'b'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> bar <span class="keyword">in</span> blue_bars:</span><br><span class="line"><span class="meta">... </span>    height = bar.get_height()</span><br><span class="line"><span class="meta">... </span>    plt.text(bar.get_x()+width/<span class="number">2</span>, height*(<span class="number">1.02</span>), height)</span><br><span class="line">...</span><br><span class="line">Text(<span class="number">0.2</span>,<span class="number">5.1</span>,<span class="string">'5'</span>)</span><br><span class="line">Text(<span class="number">1.2</span>,<span class="number">5.1</span>,<span class="string">'5'</span>)</span><br><span class="line">Text(<span class="number">2.2</span>,<span class="number">8.16</span>,<span class="string">'8'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>red_bars = plt.bar([p + width * <span class="number">2</span> <span class="keyword">for</span> p <span class="keyword">in</span> pos], red_data, width, alpha=<span class="number">0.5</span>, color=<span class="string">'r'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> bar <span class="keyword">in</span> red_bars:</span><br><span class="line"><span class="meta">... </span>    height = bar.get_height()</span><br><span class="line"><span class="meta">... </span>    plt.text(bar.get_x()+width/<span class="number">2</span>, height*(<span class="number">1.02</span>), height)</span><br><span class="line">...</span><br><span class="line">Text(<span class="number">0.4</span>,<span class="number">6.12</span>,<span class="string">'6'</span>)</span><br><span class="line">Text(<span class="number">1.4</span>,<span class="number">8.16</span>,<span class="string">'8'</span>)</span><br><span class="line">Text(<span class="number">2.4</span>,<span class="number">6.12</span>,<span class="string">'6'</span>)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/19.png">
</li>
<li><p>这里我们再举一个例子，我要画一个横着的条形图，这个图的x值都是正数，我要算出一个相对最小值百分比放在每个bar上，并且画一条竖线</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = range(<span class="number">200</span>, <span class="number">225</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bar_labels = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>, <span class="string">'e'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig = plt.figure(figsize=(<span class="number">10</span>, <span class="number">8</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y_pos = np.arange(len(data))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.yticks(y_pos, bar_labels, fontsize=<span class="number">16</span>)</span><br><span class="line">([&lt;matplotlib.axis.YTick object at <span class="number">0x124e256d8</span>&gt;, &lt;matplotlib.axis.YTick object at <span class="number">0x124d3ef60</span>&gt;, &lt;m</span><br><span class="line">atplotlib.axis.YTick object at <span class="number">0x124d30f60</span>&gt;, &lt;matplotlib.axis.YTick object at <span class="number">0x124e3b9e8</span>&gt;, &lt;matpl</span><br><span class="line">otlib.axis.YTick object at <span class="number">0x124e3bef0</span>&gt;], &lt;a list of <span class="number">5</span> Text yticklabel objects&gt;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bars = plt.barh(y_pos, data, alpha=<span class="number">0.5</span>, color=<span class="string">'g'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.vlines(min(data), <span class="number">-1</span>, len(data) + <span class="number">0.5</span>, linestyles=<span class="string">'dashed'</span>)</span><br><span class="line">&lt;matplotlib.collections.LineCollection object at <span class="number">0x124e450f0</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> bar <span class="keyword">in</span> bars:</span><br><span class="line"><span class="meta">... </span>    height = bar.get_height()</span><br><span class="line"><span class="meta">... </span>    plt.text(width*<span class="number">1.01</span>, bar.get_y()+height/<span class="number">2</span>, <span class="string">'&#123;0:.2%&#125;'</span>.format(width/min(data)))</span><br><span class="line">...</span><br><span class="line">Text(<span class="number">0.202</span>,<span class="number">0</span>,<span class="string">'0.10%'</span>)</span><br><span class="line">Text(<span class="number">0.202</span>,<span class="number">1</span>,<span class="string">'0.10%'</span>)</span><br><span class="line">Text(<span class="number">0.202</span>,<span class="number">2</span>,<span class="string">'0.10%'</span>)</span><br><span class="line">Text(<span class="number">0.202</span>,<span class="number">3</span>,<span class="string">'0.10%'</span>)</span><br><span class="line">Text(<span class="number">0.202</span>,<span class="number">4</span>,<span class="string">'0.10%'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br><span class="line"><span class="comment"># yticks: 用来画刻度</span></span><br><span class="line"><span class="comment"># vlines: 用来画竖线，需要指明x坐标，y最小值，y最大值</span></span><br></pre></td></tr></table></figure>
 <img src="/blog/2018/09/18/1/20.png">
</li>
<li><p>画一个五颜六色的图，每个bar都有一种颜色，而不是上面介绍的同一种颜色</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>mean_values = range(<span class="number">10</span>, <span class="number">18</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x_pos = range(len(mean_values))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> matplotlib.colors <span class="keyword">as</span> col</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> matplotlib.cm <span class="keyword">as</span> cm</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>cmap1 = cm.ScalarMappable(col.Normalize(min(mean_values), max(mean_values), cm.hot))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>cmap2 = cm.ScalarMappable(col.Normalize(<span class="number">0</span>, <span class="number">20</span>, cm.hot))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.subplot(<span class="number">121</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x12880c940</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.bar(x_pos, mean_values, color=cmap1.to_rgba(mean_values))</span><br><span class="line">&lt;BarContainer object of <span class="number">8</span> artists&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.subplot(<span class="number">122</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x1285c8908</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.bar(x_pos, mean_values, color=cmap2.to_rgba(mean_values))</span><br><span class="line">&lt;BarContainer object of <span class="number">8</span> artists&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>plt.show()</span><br></pre></td></tr></table></figure>
<img src="/blog/2018/09/18/1/21.png" title="color map">
</li>
<li><p>我想将每个bar的中间部分都填充不同的形状，看代码吧</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>patterns = (<span class="string">'-'</span>, <span class="string">'+'</span>, <span class="string">'x'</span>, <span class="string">'\\'</span>, <span class="string">'*'</span>, <span class="string">'o'</span>, <span class="string">'O'</span>, <span class="string">'.'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig = plt.figure(figsize=(<span class="number">10</span>, <span class="number">8</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x_pos = range(len(patterns))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mean_values = np.arange(len(patterns)) + <span class="number">1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bars = plt.bar(x_pos, mean_values, color=<span class="string">'white'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> idx, bar <span class="keyword">in</span> enumerate(bars):</span><br><span class="line"><span class="meta">... </span>    bar.set_hatch(patterns[idx])</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fig.show()</span><br></pre></td></tr></table></figure>
<img src="/blog/2018/09/18/1/22.png" title="hatch">
</li>
</ol>
<h4 id="盒图"><a href="#盒图" class="headerlink" title="盒图"></a>盒图</h4><p>那么什么是盒图呢？<br><img src="https://note.youdao.com/yws/public/resource/23dab1749cda45fe6736628d54177f02/xmlnote/WEBRESOURCE60ec20fe8a8025522d9c6d3a99fa5b2c/17189" alt="盒图"></p>
<blockquote>
<p>Q1: 总的区间的1/4位置<br>Q3: 总的区间的3/4位置<br>IQR: Q3 - Q1<br>离群点: value &lt; Q1 - 1.5 x IQR  and  value&gt; Q3 + 1.5 x IQR</p>
</blockquote>
<ol>
<li><p>知道了概念了，我们来构造一个基础的盒图</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">data = np.random.normal(<span class="number">0</span>, <span class="number">5</span>, <span class="number">100</span>)</span><br><span class="line">plt.boxplot(data)</span><br><span class="line"><span class="comment"># 这个我们构造了一个符合正太分布的数据，并用盒图展现</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>上面的基础例子我们构造的是一组数据，因此就是一个盒图，我们现在多来几组数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">data = [np.random.normal(<span class="number">0</span>, std, <span class="number">100</span>) <span class="keyword">for</span> std <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>)]</span><br><span class="line">plt.boxplot(data)</span><br></pre></td></tr></table></figure>
</li>
<li><p>在上面的基础上加上针对每个盒图的x轴额的标注</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">data = [np.random.normal(<span class="number">0</span>, std, <span class="number">100</span>) <span class="keyword">for</span> std <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>)]</span><br><span class="line">plt.boxplot(data)</span><br><span class="line">plt.xticks(np.arange(len(data)) + <span class="number">1</span>, [<span class="string">'x1'</span>, <span class="string">'x2'</span>, <span class="string">'x3'</span>])        <span class="comment"># 注意这里设置刻度的第一个参数是从1开始的，比如这里是三个图，就是 [1,2,3]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>我们还可以修改离群点的符号，默认是 +</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">plt.boxplot(data, sym=<span class="string">'s'</span>)</span><br><span class="line"><span class="comment"># 还可以修改样式，设置notch参数为True即可，自行试验吧</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>我们也可以修改盒图的方向，默认是竖着的，我们也可以横着</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">plt.boxplot(data, sym=<span class="string">'s'</span>, vert=<span class="keyword">False</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>默认情况，盒图线条是蓝色加红色，我们也同样可以设置颜色，同时盒图的body体同样可以设置颜色</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">data = [np.random.normal(<span class="number">0</span>, std, <span class="number">100</span>) <span class="keyword">for</span> std <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>)]</span><br><span class="line">bplots = plt.boxplot(data)</span><br><span class="line">plt.xticks(np.arange(len(data)) + <span class="number">1</span>, [<span class="string">'x1'</span>, <span class="string">'x2'</span>, <span class="string">'x3'</span>])</span><br><span class="line"><span class="keyword">for</span> _, components <span class="keyword">in</span> bplots.items():        <span class="comment"># 这里拿到的bplots是一个字典</span></span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> components:</span><br><span class="line">        line.set_color(<span class="string">'black'</span>)             <span class="comment"># 设置线条颜色</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 这里设置body体的颜色</span></span><br><span class="line"><span class="comment"># 在上面的基础上要做如下修改</span></span><br><span class="line">bplots = plt.boxplot(data, patch_artist=<span class="keyword">True</span>)       <span class="comment"># patch_artist参数必须设置为True才能填充颜色</span></span><br><span class="line">colors = [<span class="string">'pink'</span>, <span class="string">'lightblue'</span>, <span class="string">'lightgreen'</span>]</span><br><span class="line"><span class="keyword">for</span> bplot, color <span class="keyword">in</span> zip(bplots[<span class="string">'boxes'</span>], colors):</span><br><span class="line">    bplot.set_facecolor(color)</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="小提琴图"><a href="#小提琴图" class="headerlink" title="小提琴图"></a>小提琴图</h4><p>小提琴图与盒图表达的意义是一致的但是小提琴图相对盒图还能表示数据的集中点分布情况<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">data = [np.random.normal(<span class="number">0</span>, std, <span class="number">100</span>) <span class="keyword">for</span> std <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>)]</span><br><span class="line">vplots = plt.violinplot(data, showmedians=<span class="keyword">True</span>)</span><br><span class="line">plt.xticks(np.arange(len(data)) + <span class="number">1</span>, [<span class="string">'x1'</span>, <span class="string">'x2'</span>, <span class="string">'x3'</span>])</span><br></pre></td></tr></table></figure></p>
<p>这里我们再给你对于小提琴与与盒图的例子<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">fig, axes = plt.subplots(nrows=<span class="number">1</span>, ncols=<span class="number">2</span>)</span><br><span class="line">data = [np.random.normal(<span class="number">0</span>, std, <span class="number">100</span>) <span class="keyword">for</span> std <span class="keyword">in</span> range(<span class="number">6</span>, <span class="number">10</span>)]</span><br><span class="line">axes[<span class="number">0</span>].violinplot(data, showmedians=<span class="keyword">True</span>)</span><br><span class="line">axes[<span class="number">0</span>].set_title(<span class="string">'violin plot'</span>)</span><br><span class="line">axes[<span class="number">1</span>].boxplot(data)</span><br><span class="line">axes[<span class="number">1</span>].set_title(<span class="string">'box plot'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> ax <span class="keyword">in</span> axes:</span><br><span class="line">    ax.yaxis.grid(<span class="keyword">True</span>)</span><br><span class="line"><span class="comment">#     ax.set_xticks([y+1 for y in range(len(data))])</span></span><br><span class="line">plt.setp(axes, xticks=[y+<span class="number">1</span> <span class="keyword">for</span> y <span class="keyword">in</span> range(len(data))], xticklabels=[<span class="string">'x1'</span>, <span class="string">'x2'</span>, <span class="string">'x3'</span>, <span class="string">'x4'</span>])</span><br></pre></td></tr></table></figure></p>
<h4 id="直方图"><a href="#直方图" class="headerlink" title="直方图"></a>直方图</h4><ol>
<li><p>一个基础的直方图</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> math</span><br><span class="line">data = np.random.normal(<span class="number">0</span>, <span class="number">20</span>, <span class="number">1000</span>)</span><br><span class="line">bins = np.arange(math.floor(min(data)), math.ceil(max(data)), <span class="number">5</span>)</span><br><span class="line">plt.hist(data, bins)</span><br></pre></td></tr></table></figure>
</li>
<li><p>上面就一组数据，那如果有两组数据应该怎么画与区分呢</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line">data1 = [random.gauss(<span class="number">15</span>, <span class="number">10</span>) <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">500</span>)]</span><br><span class="line">data2 = [random.gauss(<span class="number">5</span>, <span class="number">5</span>) <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">500</span>)]</span><br><span class="line">bins = np.arange(<span class="number">-50</span>, <span class="number">50</span>, <span class="number">2.5</span>)</span><br><span class="line">plt.hist(data1, bins=bins, label=<span class="string">'class1'</span>, alpha=<span class="number">0.3</span>)</span><br><span class="line">plt.hist(data2, bins=bins, label=<span class="string">'class2'</span>, alpha=<span class="number">0.3</span>)</span><br><span class="line">plt.legend(loc=<span class="string">'best'</span>)</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="散点图"><a href="#散点图" class="headerlink" title="散点图"></a>散点图</h4><ol>
<li><p>一个基础的散点图</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">x = np.random.normal(<span class="number">0</span>, <span class="number">1</span>, <span class="number">1000</span>)</span><br><span class="line">y = np.random.normal(<span class="number">0</span>, <span class="number">1</span>, <span class="number">1000</span>)</span><br><span class="line">plt.scatter(x, y, alpha=<span class="number">0.3</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>多组数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">mu_vec1 = np.array([<span class="number">0</span>, <span class="number">0</span>])</span><br><span class="line">cov_mat1 = np.array([[<span class="number">2</span>,<span class="number">0</span>],[<span class="number">0</span>,<span class="number">2</span>]])</span><br><span class="line"></span><br><span class="line">x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, <span class="number">100</span>)</span><br><span class="line">x2_samples = np.random.multivariate_normal(mu_vec1 + <span class="number">0.2</span>, cov_mat1 + <span class="number">0.2</span>, <span class="number">100</span>)</span><br><span class="line">x3_samples = np.random.multivariate_normal(mu_vec1 + <span class="number">0.4</span>, cov_mat1 + <span class="number">0.4</span>, <span class="number">100</span>)</span><br><span class="line">plt.scatter(x1_samples[:,<span class="number">0</span>], x1_samples[:, <span class="number">1</span>], marker=<span class="string">'x'</span>, color=<span class="string">'blue'</span>, alpha=<span class="number">0.6</span>, label=<span class="string">'x1'</span>)     <span class="comment"># 分别传入x数组，y数组</span></span><br><span class="line">plt.scatter(x2_samples[:,<span class="number">0</span>], x2_samples[:, <span class="number">1</span>], marker=<span class="string">'o'</span>, color=<span class="string">'red'</span>, alpha=<span class="number">0.6</span>, label=<span class="string">'x2'</span>)</span><br><span class="line">plt.scatter(x3_samples[:,<span class="number">0</span>], x3_samples[:, <span class="number">1</span>], marker=<span class="string">'^'</span>, color=<span class="string">'green'</span>, alpha=<span class="number">0.6</span>, label=<span class="string">'x3'</span>)</span><br><span class="line">plt.legend(loc=<span class="string">'best'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>对于点不多的情况，我可能要将对应点的坐标写进去</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">x_coords = np.random.rand(<span class="number">10</span>)</span><br><span class="line">y_coords = np.random.rand(<span class="number">10</span>)</span><br><span class="line">plt.scatter(x_coords, y_coords)</span><br><span class="line"><span class="keyword">for</span> x, y <span class="keyword">in</span> zip(x_coords, y_coords):</span><br><span class="line">    plt.annotate(<span class="string">'(%.2f,%.2f)'</span>%(x,y), xy=(x,y), xytext=(<span class="number">0</span>,<span class="number">-15</span>), textcoords=<span class="string">'offset points'</span>, ha=<span class="string">'center'</span>)</span><br><span class="line"><span class="comment"># textcoords: 对于这种画坐标点的要设置该参数，其他不需要，可以自行测试</span></span><br><span class="line"><span class="comment"># xy: 要标记的坐标点</span></span><br><span class="line"><span class="comment"># xytext: 偏移位置</span></span><br><span class="line"><span class="comment"># ha: 对齐方式</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>我们还可以调整点的大小</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">plt.scatter(x_coords, y_coords, s=<span class="number">50</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>通过距离设置点的大小，实现一种黑洞的效果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mu_vec1 = np.array([<span class="number">0</span>,<span class="number">0</span>])</span><br><span class="line">cov_mat1 = np.array([[<span class="number">1</span>,<span class="number">0</span>],[<span class="number">0</span>,<span class="number">1</span>]])</span><br><span class="line">X = np.random.multivariate_normal(mu_vec1, cov_mat1, <span class="number">500</span>)</span><br><span class="line">fig = plt.figure(figsize=(<span class="number">8</span>,<span class="number">6</span>))</span><br><span class="line">R = X ** <span class="number">2</span></span><br><span class="line">R_sum = R.sum(axis=<span class="number">1</span>)</span><br><span class="line">plt.scatter(X[:,<span class="number">0</span>], X[:,<span class="number">1</span>], color=<span class="string">'grey'</span>, marker=<span class="string">'o'</span>, s=<span class="number">20</span> * R_sum, alpha=<span class="number">0.5</span>)</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="3D图"><a href="#3D图" class="headerlink" title="3D图"></a>3D图</h4><p>相比2维图，3维图多了一个包 <code>from mpl_toolkits.mplot3d import Axes3D</code></p>
<ol>
<li><p>一个基本的3D图</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用Axes3D函数</span></span><br><span class="line"><span class="keyword">from</span> mpl_toolkits.mplot3d <span class="keyword">import</span> Axes3D</span><br><span class="line">fig = plt.figure()</span><br><span class="line">ax = Axes3D(fig)            <span class="comment"># 将2D转为一个3D</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用gca函数</span></span><br><span class="line"><span class="keyword">from</span> mpl_toolkits.mplot3d <span class="keyword">import</span> Axes3D</span><br><span class="line">fig = plt.figure()</span><br><span class="line">ax = fig.gca(projection=<span class="string">'3d'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用add_subplot</span></span><br><span class="line"><span class="keyword">from</span> mpl_toolkits.mplot3d <span class="keyword">import</span> Axes3D</span><br><span class="line">fig = plt.figure()</span><br><span class="line">ax = fig.add_subplot(<span class="number">111</span>, projection=<span class="string">'3d'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 总结一下，总之都要拿到ax，然后我们利用ax画图</span></span><br><span class="line"><span class="comment"># 示例一</span></span><br><span class="line">theta = np.linspace(<span class="number">-4</span> * np.pi, <span class="number">4</span> * np.pi, <span class="number">100</span>)</span><br><span class="line">z = np.linspace(<span class="number">-2</span>, <span class="number">2</span>, <span class="number">100</span>)</span><br><span class="line">r = z ** <span class="number">2</span> + <span class="number">1</span></span><br><span class="line">x = r * np.sin(theta)</span><br><span class="line">y = r * np.cos(theta)</span><br><span class="line">ax.plot(x, y, z)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 示例二</span></span><br><span class="line">x = np.arange(<span class="number">-4</span>, <span class="number">4</span>, <span class="number">0.25</span>)</span><br><span class="line">y = np.arange(<span class="number">-4</span>, <span class="number">4</span>, <span class="number">0.25</span>)</span><br><span class="line">X, Y = np.meshgrid(x, y)</span><br><span class="line">Z = np.sin(np.sqrt(X ** <span class="number">2</span> + Y ** <span class="number">2</span>))</span><br><span class="line">ax.plot_surface(X, Y, Z, rstride=<span class="number">1</span>, cstride=<span class="number">1</span>, cmap=<span class="string">'rainbow'</span>)</span><br><span class="line"><span class="comment"># rstride: 行宽度</span></span><br><span class="line"><span class="comment"># cstride: 列宽度</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>画一个三维的包含两组数据的散点图，设置不同颜色</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">x = np.arange(<span class="number">-4</span>, <span class="number">4</span>, <span class="number">0.25</span>)</span><br><span class="line">y = np.arange(<span class="number">-4</span>, <span class="number">4</span>, <span class="number">0.25</span>)</span><br><span class="line">X, Y = np.meshgrid(x, y)</span><br><span class="line">Z = np.sin(np.sqrt(X ** <span class="number">2</span> + Y ** <span class="number">2</span>))</span><br><span class="line">ax.plot_surface(X, Y, Z, rstride=<span class="number">1</span>, cstride=<span class="number">1</span>, cmap=<span class="string">'rainbow'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>三维的图都有一个视角，我们如何改变视角呢</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ax.view_init(<span class="number">20</span>, <span class="number">0</span>)         <span class="comment"># 参数值决定了视角方向，自己试验吧</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>我们画一个三维的条形图</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">fig = plt.figure()</span><br><span class="line">ax = fig.add_subplot(<span class="number">111</span>, projection=<span class="string">'3d'</span>)</span><br><span class="line"><span class="keyword">for</span> c, z <span class="keyword">in</span> zip([<span class="string">'r'</span>, <span class="string">'g'</span>, <span class="string">'b'</span>, <span class="string">'y'</span>], [<span class="number">30</span>, <span class="number">20</span>, <span class="number">10</span>, <span class="number">0</span>]):</span><br><span class="line">    xs = np.arange(<span class="number">20</span>)</span><br><span class="line">    ys = np.random.rand(<span class="number">20</span>)</span><br><span class="line">    ax.bar(xs, ys, z, zdir=<span class="string">'y'</span>, color=c, alpha=<span class="number">0.5</span>)         <span class="comment"># zdir默认值是z，可能会觉得方向不对，自行调整为y即可</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>画投影</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">fig = plt.figure()</span><br><span class="line">ax = fig.gca(projection=<span class="string">'3d'</span>)</span><br><span class="line">x = np.arange(<span class="number">-4</span>, <span class="number">4</span>, <span class="number">0.25</span>)</span><br><span class="line">y = np.arange(<span class="number">-4</span>, <span class="number">4</span>, <span class="number">0.25</span>)</span><br><span class="line">X, Y = np.meshgrid(x, y)</span><br><span class="line">Z = np.sin(np.sqrt(X ** <span class="number">2</span> + Y ** <span class="number">2</span>))</span><br><span class="line">ax.plot_surface(X, Y, Z, rstride=<span class="number">1</span>, cstride=<span class="number">1</span>, cmap=<span class="string">'rainbow'</span>)</span><br><span class="line">ax.contour(X, Y, Z, offset=<span class="number">-2</span>, cmap=<span class="string">'rainbow'</span>)          <span class="comment"># 画投影</span></span><br><span class="line">ax.set_zlim(<span class="number">-2</span>, <span class="number">2</span>)          <span class="comment"># 设置z的范围</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="pie图"><a href="#pie图" class="headerlink" title="pie图"></a>pie图</h4><ol>
<li><p>一个简单的pie图<br>pie图用于直观得查看比例的</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">m = <span class="number">51212</span></span><br><span class="line">f = <span class="number">40742</span></span><br><span class="line">m_perc = m / (m+f)</span><br><span class="line">f_perc = f / (m+f)</span><br><span class="line">colors = [<span class="string">'navy'</span>, <span class="string">'lightcoral'</span>]</span><br><span class="line">labels = [<span class="string">'Male'</span>, <span class="string">'Female'</span>]</span><br><span class="line">plt.figure(figsize=(<span class="number">8</span>, <span class="number">8</span>))</span><br><span class="line">paches, texts, autotexts = plt.pie([m_perc, f_perc], labels=labels, autopct=<span class="string">'%0.10f%%'</span>, explode=[<span class="number">0</span>, <span class="number">0.1</span>], colors=colors)</span><br><span class="line"><span class="comment"># autopct: 这里的值比较特别，就是表示百分比，可以设置显示精度，偏移大小</span></span><br><span class="line"><span class="comment"># explode: 控制缝隙大小</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>上面的基础图太丑了，我们可以美化一下</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> text <span class="keyword">in</span> texts + autotexts:</span><br><span class="line">    text.set_fontsize(<span class="number">20</span>)</span><br><span class="line"><span class="keyword">for</span> text <span class="keyword">in</span> autotexts:</span><br><span class="line">    text.set_color(<span class="string">'white'</span>)</span><br><span class="line"><span class="comment"># texts: 表示labels</span></span><br><span class="line"><span class="comment"># autotexts: 表示百分比的文字</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="一些绘图细节设置"><a href="#一些绘图细节设置" class="headerlink" title="一些绘图细节设置"></a>一些绘图细节设置</h3><h4 id="关于轴的设置"><a href="#关于轴的设置" class="headerlink" title="关于轴的设置"></a>关于轴的设置</h4><ol>
<li><p>一个默认的图的轴样式，自行执行查看</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">x = range(<span class="number">10</span>)</span><br><span class="line">y = range(<span class="number">10</span>)</span><br><span class="line">plt.plot(x, y)</span><br></pre></td></tr></table></figure>
</li>
<li><p>现在要将x轴与y轴的刻度去掉</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">x = range(<span class="number">10</span>)</span><br><span class="line">y = range(<span class="number">10</span>)</span><br><span class="line">fig = plt.gca()</span><br><span class="line">plt.plot(x, y)</span><br><span class="line">fig.axes.get_xaxis().set_visible(<span class="keyword">False</span>)</span><br><span class="line">fig.axes.get_yaxis().set_visible(<span class="keyword">False</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>我们以直方图的例子，来进行美化</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> math</span><br><span class="line">x = np.random.normal(loc=<span class="number">0.0</span>, scale=<span class="number">1.0</span>, size=<span class="number">300</span>)</span><br><span class="line">width = <span class="number">0.5</span></span><br><span class="line">bins = np.arange(math.floor(x.min())-width, math.ceil(x.max())+width, width)</span><br><span class="line">ax = plt.subplot(<span class="number">111</span>)</span><br><span class="line">ax.spines[<span class="string">'top'</span>].set_visible(<span class="keyword">False</span>)         <span class="comment"># 删除上边的轴线</span></span><br><span class="line">ax.spines[<span class="string">'right'</span>].set_visible(<span class="keyword">False</span>)       <span class="comment"># 删除右边的轴线</span></span><br><span class="line">plt.hist(x, alpha=<span class="number">0.5</span>, bins=bins)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 上面的轴线不见了，但是刻度还在，我们这里看看如何删除刻度</span></span><br><span class="line">plt.tick_params(top=<span class="keyword">False</span>, right=<span class="keyword">False</span>)</span><br><span class="line"><span class="comment"># 网格加上</span></span><br><span class="line">plt.grid(<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>有时我们会设置一些x轴的labels，但是这些label特别长，我们需要美化</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 优化前</span></span><br><span class="line">x = range(<span class="number">10</span>)</span><br><span class="line">y = range(<span class="number">10</span>)</span><br><span class="line">labels = [<span class="string">'jackstraw'</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>)]</span><br><span class="line">fig, ax = plt.subplots()</span><br><span class="line">plt.plot(x, y)</span><br><span class="line">ax.set_xticklabels(labels)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 优化后</span></span><br><span class="line">ax.set_xticklabels(labels, rotation=<span class="number">45</span>, horizontalalignment=<span class="string">'left'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>有时我们画了多条不同颜色的线，我们需要区分各自表达的意义</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">x = np.arange(<span class="number">10</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>):</span><br><span class="line">    plt.plot(x, i * x ** <span class="number">2</span>, label=<span class="string">'Group %d'</span> % i)       <span class="comment"># 后面还可以添加 marker='o' 的参数，自己试验一下有什么不一样</span></span><br><span class="line">plt.legend()        <span class="comment"># 使用这个函数，便会通道对应label值来区分</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 上面的指示器位置也是可以调整的，有时可能会挡住数据</span></span><br><span class="line">plt.legend(loc=<span class="string">'best'</span>)          <span class="comment"># 自动选择位置</span></span><br><span class="line"><span class="comment"># 也可以自己设定位置</span></span><br><span class="line">plt.legend(loc=<span class="string">'center left'</span>)   <span class="comment"># 自己查找位置</span></span><br><span class="line"><span class="comment"># 也可以指定在图外面</span></span><br><span class="line">plt.legend(loc=<span class="string">'upper center'</span>, bbox_to_anchor=(<span class="number">0.5</span>,<span class="number">1.15</span>), ncol=<span class="number">3</span>)   </span><br><span class="line"><span class="comment"># 竖着盛放</span></span><br><span class="line">plt.legend(loc=<span class="string">'upper center'</span>, bbox_to_anchor=(<span class="number">1.15</span>,<span class="number">1</span>), ncol=<span class="number">1</span>)</span><br><span class="line"><span class="comment"># 如果非要放某个位置，又会挡住图形，可以设置透明度</span></span><br><span class="line">plt.legend(loc=<span class="string">'upper right'</span>, framealpha=<span class="number">0.1</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>可以设置全局参数，所有图形都生效</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 比如我们要修改title的字体大小</span></span><br><span class="line"><span class="keyword">import</span> matplotlib <span class="keyword">as</span> mpl</span><br><span class="line">mpl.rcParams[<span class="string">'axes.titlesize'</span>] = <span class="number">30</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="关于子图布局"><a href="#关于子图布局" class="headerlink" title="关于子图布局"></a>关于子图布局</h4><p>布局这一块，在前面的例子我们涉及了一些简单的布局</p>
<p>比如在subplot函数中指定布局参数111便指画一个图，121指画一行两列的第一个图</p>
<p>这种方式比较基础，可定制性比较差，这里我们介绍使用网格布局</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ax1 = plt.subplot2grid((<span class="number">3</span>,<span class="number">3</span>), (<span class="number">0</span>,<span class="number">0</span>))</span><br><span class="line">ax2 = plt.subplot2grid((<span class="number">3</span>,<span class="number">3</span>), (<span class="number">1</span>,<span class="number">0</span>))</span><br><span class="line">ax3 = plt.subplot2grid((<span class="number">3</span>,<span class="number">3</span>), (<span class="number">0</span>,<span class="number">2</span>), rowspan=<span class="number">3</span>)     <span class="comment"># 指定了位置与所占行数</span></span><br><span class="line">ax4 = plt.subplot2grid((<span class="number">3</span>,<span class="number">3</span>), (<span class="number">2</span>,<span class="number">0</span>), colspan=<span class="number">2</span>)     <span class="comment"># 指定了位置与所占列数</span></span><br><span class="line">ax1 = plt.subplot2grid((<span class="number">3</span>,<span class="number">3</span>), (<span class="number">0</span>,<span class="number">1</span>), rowspan=<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 试验一下吧</span></span><br></pre></td></tr></table></figure>
<p>我们还可以画一个嵌套的图</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">x = np.linspace(<span class="number">0</span>, <span class="number">10</span>, <span class="number">1000</span>)</span><br><span class="line">y1 = x**<span class="number">2</span></span><br><span class="line">y2 = np.sin(x**<span class="number">2</span>)</span><br><span class="line">fig, ax = plt.subplots()</span><br><span class="line">left, bottom, width, height = [<span class="number">0.22</span>, <span class="number">0.45</span>, <span class="number">0.3</span>, <span class="number">0.35</span>]</span><br><span class="line">sub_ax = fig.add_axes([left, bottom, width, height])</span><br><span class="line">ax.plot(x, y1)</span><br><span class="line">sub_ax.plot(x, y2)</span><br></pre></td></tr></table></figure>
<p>使用另外一种画子图的方式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> mpl_toolkits.axes_grid1.inset_locator <span class="keyword">import</span> inset_axes</span><br><span class="line"></span><br><span class="line">top10_arrivals_countries = [<span class="string">'CANADA'</span>,<span class="string">'MEXICO'</span>,<span class="string">'UNITED\nKINGDOM'</span>,\</span><br><span class="line">                            <span class="string">'JAPAN'</span>,<span class="string">'CHINA'</span>,<span class="string">'GERMANY'</span>,<span class="string">'SOUTH\nKOREA'</span>,\</span><br><span class="line">                            <span class="string">'FRANCE'</span>,<span class="string">'BRAZIL'</span>,<span class="string">'AUSTRALIA'</span>]</span><br><span class="line">top10_arrivals_values = [<span class="number">16.625687</span>, <span class="number">15.378026</span>, <span class="number">3.934508</span>, <span class="number">2.999718</span>,\</span><br><span class="line">                         <span class="number">2.618737</span>, <span class="number">1.769498</span>, <span class="number">1.628563</span>, <span class="number">1.419409</span>,\</span><br><span class="line">                         <span class="number">1.393710</span>, <span class="number">1.136974</span>]</span><br><span class="line">arrivals_countries = [<span class="string">'WESTERN\nEUROPE'</span>,<span class="string">'ASIA'</span>,<span class="string">'SOUTH\nAMERICA'</span>,\</span><br><span class="line">                      <span class="string">'OCEANIA'</span>,<span class="string">'CARIBBEAN'</span>,<span class="string">'MIDDLE\nEAST'</span>,\</span><br><span class="line">                      <span class="string">'CENTRAL\nAMERICA'</span>,<span class="string">'EASTERN\nEUROPE'</span>,<span class="string">'AFRICA'</span>]</span><br><span class="line">arrivals_percent = [<span class="number">36.9</span>,<span class="number">30.4</span>,<span class="number">13.8</span>,<span class="number">4.4</span>,<span class="number">4.0</span>,<span class="number">3.6</span>,<span class="number">2.9</span>,<span class="number">2.6</span>,<span class="number">1.5</span>]</span><br><span class="line"></span><br><span class="line">fig, ax = plt.subplots(figsize=(<span class="number">20</span>, <span class="number">12</span>))</span><br><span class="line">bars = ax.bar(range(<span class="number">10</span>), top10_arrivals_values, color=<span class="string">'blue'</span>)</span><br><span class="line"><span class="keyword">for</span> rect <span class="keyword">in</span> bars:</span><br><span class="line">    height = rect.get_height()</span><br><span class="line">    ax.text(rect.get_x() + rect.get_width()/<span class="number">2</span>, <span class="number">1.02</span>*height, <span class="string">'&#123;:,&#125;'</span>.format(float(height)), ha=<span class="string">'center'</span>, va=<span class="string">'bottom'</span>, fontsize=<span class="number">18</span>)</span><br><span class="line">plt.xticks(range(<span class="number">10</span>), top10_arrivals_countries, fontsize=<span class="number">18</span>)    <span class="comment"># 注意这里修改刻度的位置，如果下载最后面，则修改的是子图的刻度</span></span><br><span class="line">sub_ax = inset_axes(ax, width=<span class="number">6</span>, height=<span class="number">6</span>, loc=<span class="number">5</span>)               <span class="comment"># width与height指定了子图的宽高，loc指定了位置，自己试验吧</span></span><br><span class="line">explode = (<span class="number">0.08</span>, <span class="number">0.08</span>, <span class="number">0.05</span>, <span class="number">0.05</span>,<span class="number">0.05</span>,<span class="number">0.05</span>,<span class="number">0.05</span>,<span class="number">0.05</span>,<span class="number">0.05</span>)</span><br><span class="line">sub_ax.pie(arrivals_percent, labels=arrivals_countries, autopct=<span class="string">'%1.1f%%'</span>, explode=explode) <span class="comment"># 这里画出来的pie字体非常小，文字也不清楚，自己修正一下试试</span></span><br><span class="line"><span class="keyword">for</span> spine <span class="keyword">in</span> ax.spines.values():</span><br><span class="line">    spine.set_visible(<span class="keyword">False</span>)        <span class="comment"># 去掉所有轴</span></span><br></pre></td></tr></table></figure>
<p>matplotlib也可以画图形，要是感兴趣可以关注一下 <code>from matplotlib.patches import Circle, Wedge, Polygon, Ellipse</code></p>
<h2 id="高级篇"><a href="#高级篇" class="headerlink" title="高级篇"></a>高级篇</h2><p>讲了这么多，让我们看看matplot如何与pandas与sklearn结合</p>
<ol>
<li>这里我们构造一个拥有3个指标20个记录的DataFrame，然后看看针对这些数据，我们能够画成什么样的图</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line">np.random.seed(<span class="number">0</span>)</span><br><span class="line">df = pd.DataFrame(&#123;<span class="string">'Condition 1'</span>: np.random.rand(<span class="number">20</span>),</span><br><span class="line">                  <span class="string">'Condition 2'</span>: np.random.rand(<span class="number">20</span>) * <span class="number">0.9</span>,</span><br><span class="line">                  <span class="string">'Condition 3'</span>: np.random.rand(<span class="number">20</span>) * <span class="number">1.1</span>&#125;)</span><br><span class="line">fig, ax = plt.subplots()            <span class="comment"># 获取画图的面板</span></span><br><span class="line">df.plot.bar(ax=ax)    <span class="comment"># DataFrame直接使用plot对象进行画图，指定面板即可</span></span><br><span class="line"><span class="comment"># 尝试添加一个参数 df.plot.bar(ax=ax, stacked=True)，看看结果吧</span></span><br></pre></td></tr></table></figure>
<ol>
<li><p>上面的例子试验了么？最后一个注释的结果也看到了么？假设我们想要看到没组数据的百分比情况，应该怎么做呢？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在上一个的数据基础上</span></span><br><span class="line"><span class="keyword">from</span> matplotlib.ticker <span class="keyword">import</span> FuncFormatter</span><br><span class="line">np.random.seed(<span class="number">0</span>)</span><br><span class="line">df = pd.DataFrame(&#123;<span class="string">'Condition 1'</span>: np.random.rand(<span class="number">20</span>),</span><br><span class="line">                  <span class="string">'Condition 2'</span>: np.random.rand(<span class="number">20</span>) * <span class="number">0.9</span>,</span><br><span class="line">                  <span class="string">'Condition 3'</span>: np.random.rand(<span class="number">20</span>) * <span class="number">1.1</span>&#125;)</span><br><span class="line">df_ratio = df.div(df.sum(axis=<span class="number">1</span>), axis=<span class="number">0</span>)</span><br><span class="line">fig, ax = plt.subplots()</span><br><span class="line">df_ratio.plot.bar(ax=ax, stacked=<span class="keyword">True</span>)</span><br><span class="line">ax.yaxis.set_major_formatter(FuncFormatter(<span class="keyword">lambda</span> y, _: <span class="string">'&#123;:.0%&#125;'</span>.format(y)))</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用sklean的PCA取出重要指标进行绘图</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">from</span> sklearn.decomposition <span class="keyword">import</span> PCA</span><br><span class="line"><span class="keyword">from</span> mpl_toolkits.mplot3d <span class="keyword">import</span> Axes3D</span><br><span class="line"><span class="keyword">from</span> sklearn.preprocessing <span class="keyword">import</span> Imputer</span><br><span class="line"></span><br><span class="line">df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line">obj_df = df.select_dtypes(include=[<span class="string">'object'</span>])</span><br><span class="line">df.drop(obj_df.columns, axis=<span class="number">1</span>, inplace=<span class="keyword">True</span>)   <span class="comment"># 我们必须删掉object类型的数据，Imputer才能处理，否则会报错</span></span><br><span class="line">impute = df.DataFrame(Imputer().fit_transform(df))  <span class="comment"># 拿到一个带有缺失值的DataFrame</span></span><br><span class="line">impute.columns = df.columns</span><br><span class="line">imputer.index = df.index</span><br><span class="line"></span><br><span class="line"><span class="comment"># 我们使用是否存活为标签，因此要删除存活标签作为指标</span></span><br><span class="line">features = impute.drop(<span class="string">'Survived'</span>, axis=<span class="number">1</span>)</span><br><span class="line">y = impute[<span class="string">'Survived'</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 开始计算主成分</span></span><br><span class="line">pca = PCA(n_components=<span class="number">3</span>)</span><br><span class="line">X_r = pca.fit_transform(features)</span><br><span class="line"></span><br><span class="line">print(<span class="string">"Explained variance: \nPC1 &#123;:.2%&#125;\nPC2 &#123;:.2%&#125;\nPC3 &#123;:.2%&#125;"</span>.format(pca.explained_variance_ratio_[<span class="number">0</span>],</span><br><span class="line">               pca.explained_variance_ratio_[<span class="number">1</span>],</span><br><span class="line">               pca.explained_variance_ratio_[<span class="number">2</span>]))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 画图</span></span><br><span class="line">fig = plt.figure()</span><br><span class="line">ax = Axes3D(fig)</span><br><span class="line">ax.scatter(X_r[:, <span class="number">0</span>], X_r[:, <span class="number">1</span>], X_r[:, <span class="number">2</span>], c=y, cmap=plt.cm.coolwarm)</span><br><span class="line"></span><br><span class="line">ax.set_xlabel(<span class="string">'PC1'</span>)</span><br><span class="line">ax.set_xlabel(<span class="string">'PC2'</span>)</span><br><span class="line">ax.set_xlabel(<span class="string">'PC3'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>可选的样式</p>
</li>
</ol>
<div class="table-container">
<table>
<thead>
<tr>
<th>字符</th>
<th>类型</th>
<th>字符</th>
<th>类型</th>
</tr>
</thead>
<tbody>
<tr>
<td>‘-‘</td>
<td></td>
<td>‘—‘</td>
<td></td>
</tr>
<tr>
<td>‘-.’</td>
<td></td>
<td>‘:’</td>
<td></td>
</tr>
<tr>
<td>‘.’</td>
<td></td>
<td>‘,’</td>
<td>像素点</td>
</tr>
<tr>
<td>‘o’</td>
<td></td>
<td>‘v’</td>
<td></td>
</tr>
<tr>
<td>‘^’</td>
<td></td>
<td>‘&lt;’</td>
<td></td>
</tr>
<tr>
<td>‘&gt;’</td>
<td></td>
<td>‘1’</td>
<td></td>
</tr>
<tr>
<td>‘2’</td>
<td></td>
<td>‘3’</td>
<td></td>
</tr>
<tr>
<td>‘4’</td>
<td></td>
<td>‘s’</td>
<td></td>
</tr>
<tr>
<td>‘p’</td>
<td></td>
<td>‘*’</td>
<td></td>
</tr>
<tr>
<td>‘h’</td>
<td></td>
<td>‘H’</td>
<td></td>
</tr>
<tr>
<td>‘+’</td>
<td></td>
<td>‘x’</td>
<td></td>
</tr>
<tr>
<td>‘D’</td>
<td></td>
<td>‘d’</td>
<td></td>
</tr>
<tr>
<td>‘_’</td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/09/15/2.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/09/15/2.html" itemprop="url">数据分析之pandas</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-15T19:55:45+08:00">
                2018-09-15
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/" itemprop="url" rel="index">
                    <span itemprop="name">数据分析</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/基础工具/" itemprop="url" rel="index">
                    <span itemprop="name">基础工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/09/15/2.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/09/15/2.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/09/15/2.html" class="leancloud_visitors" data-flag-title="数据分析之pandas">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="pandas"><a href="#pandas" class="headerlink" title="pandas"></a>pandas</h1><p>pandas是一个开源的、高性能、易于使用的用于数据结构化与分析python第三方库。本文是涉及比较常见的padans操作，详情清参考<a href="http://pandas.pydata.org/pandas-docs/stable/">pandas官网文档</a></p>
<h2 id="数据准备"><a href="#数据准备" class="headerlink" title="数据准备"></a>数据准备</h2><p>为了便于练习，本文会以泰坦尼克号数据为例</p>
<ol>
<li><a href="https://pan.baidu.com/s/1TRZr824a7tVcpF1hSOlr5A">titanic_train.csv</a> 密码: hrmw</li>
</ol>
<h2 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h2><p>pandas 基于两种数据类型：Series与DataFrame</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Series: 一个一维的数据类型，其中每一个元素都有一个标签（即索引，可以是数字或字符串）</span><br><span class="line">DataFrame: 一个二维的表结构。DataFrame可以存储许多种不同的数据类型，且每一个坐标轴都有自己的标签</span><br></pre></td></tr></table></figure>
<h3 id="关于DataFrame的基本操作"><a href="#关于DataFrame的基本操作" class="headerlink" title="关于DataFrame的基本操作"></a>关于DataFrame的基本操作</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = &#123;</span><br><span class="line"><span class="meta">... </span>    <span class="string">'country'</span>: [<span class="string">'beijing'</span>, <span class="string">'chengdu'</span>, <span class="string">'shanghai'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'population'</span>: [<span class="number">10</span>, <span class="number">12</span>, <span class="number">14</span>]</span><br><span class="line"><span class="meta">... </span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(data)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">    country  population</span><br><span class="line"><span class="number">0</span>   beijing          <span class="number">10</span></span><br><span class="line"><span class="number">1</span>   chengdu          <span class="number">12</span></span><br><span class="line"><span class="number">2</span>  shanghai          <span class="number">14</span></span><br></pre></td></tr></table></figure>
<p>可以看到，构造一个DataFrame需要指定一个字典，字典的键表示列名，字典的值表示列数据，DataFrame有一些基础的属性与方法供我们使用。</p>
<div><div class="fold_hider"><div class="close hider_title">常用属性与方法实例</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们就简单看一下数据的结构，可是使用head/tail函数，参数表示显示的条目数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.head(<span class="number">1</span>)</span><br><span class="line">   country  population</span><br><span class="line"><span class="number">0</span>  beijing          <span class="number">10</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.tail(<span class="number">1</span>)</span><br><span class="line">    country  population</span><br><span class="line"><span class="number">2</span>  shanghai          <span class="number">14</span></span><br><span class="line"><span class="comment"># 我们也可以通过info函数粗略看一下这个数据的各类情况，比如：索引的范围、列的情况、数据类型以及占用的内存大小等</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.info()</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> '<span class="title">pandas</span>.<span class="title">core</span>.<span class="title">frame</span>.<span class="title">DataFrame</span>'&gt;</span></span><br><span class="line"><span class="class"><span class="title">RangeIndex</span>:</span> <span class="number">3</span> entries, <span class="number">0</span> to <span class="number">2</span></span><br><span class="line">Data columns (total <span class="number">2</span> columns):</span><br><span class="line">country       <span class="number">3</span> non-null object</span><br><span class="line">population    <span class="number">3</span> non-null int64</span><br><span class="line">dtypes: int64(<span class="number">1</span>), object(<span class="number">1</span>)</span><br><span class="line">memory usage: <span class="number">128.0</span>+ bytes</span><br><span class="line"><span class="comment"># 我们可以获取DataFrame的索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.index</span><br><span class="line">RangeIndex(start=<span class="number">0</span>, stop=<span class="number">3</span>, step=<span class="number">1</span>)</span><br><span class="line"><span class="comment"># 获取DataFrame的所有列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.columns</span><br><span class="line">Index([<span class="string">'country'</span>, <span class="string">'population'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"><span class="comment"># 获取ndarray的数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.values</span><br><span class="line">array([[<span class="string">'beijing'</span>, <span class="number">10</span>],</span><br><span class="line">       [<span class="string">'chengdu'</span>, <span class="number">12</span>],</span><br><span class="line">       [<span class="string">'shanghai'</span>, <span class="number">14</span>]], dtype=object)</span><br><span class="line"><span class="comment"># 获取每一个列的数据类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.dtypes</span><br><span class="line">country       object</span><br><span class="line">population     int64</span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 快速获取各类指标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.describe()</span><br><span class="line">       population</span><br><span class="line">count         <span class="number">3.0</span></span><br><span class="line">mean         <span class="number">12.0</span></span><br><span class="line">std           <span class="number">2.0</span></span><br><span class="line">min          <span class="number">10.0</span></span><br><span class="line"><span class="number">25</span>%          <span class="number">11.0</span></span><br><span class="line"><span class="number">50</span>%          <span class="number">12.0</span></span><br><span class="line"><span class="number">75</span>%          <span class="number">13.0</span></span><br><span class="line">max          <span class="number">14.0</span></span><br></pre></td></tr></table></figure>

</div></div>
<p>读取<code>titanic_train.csv</code>文件，转化为DataFrame</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 直接使用read_csv函数读取csv文件即可转换为DataFrame，可以根据上面试验的函数与属性自行试验一下，这个数据集的基本信息</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.head(<span class="number">2</span>)</span><br><span class="line">   PassengerId  Survived  Pclass                                               Name     Sex   Age  SibSp  Parch     Ticket     Fare Cabin Embarked</span><br><span class="line"><span class="number">0</span>            <span class="number">1</span>         <span class="number">0</span>       <span class="number">3</span>                            Braund, Mr. Owen Harris    male  <span class="number">22.0</span>      <span class="number">1</span>      <span class="number">0</span>  A/<span class="number">5</span> <span class="number">21171</span>   <span class="number">7.2500</span>   NaN        S</span><br><span class="line"><span class="number">1</span>            <span class="number">2</span>         <span class="number">1</span>       <span class="number">1</span>  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  <span class="number">38.0</span>      <span class="number">1</span>      <span class="number">0</span>   PC <span class="number">17599</span>  <span class="number">71.2833</span>   C85        C</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">访问DataFrame示例</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 可以获取DataFrame中的一列，可以看到获取的这一列是一个Series结构，Series结构的讲解参见下文。该结构很多属性与DataFrame是类似的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age = df[<span class="string">'Age'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.head(<span class="number">2</span>)</span><br><span class="line"><span class="number">0</span>    <span class="number">22.0</span></span><br><span class="line"><span class="number">1</span>    <span class="number">38.0</span></span><br><span class="line">Name: Age, dtype: float64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>type(age)</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> '<span class="title">pandas</span>.<span class="title">core</span>.<span class="title">series</span>.<span class="title">Series</span>'&gt;</span></span><br><span class="line"><span class="class"># 我们也可以按索引来访问，可以发现这种访问方式或出错，获取某个列的时候，我们是通过 <span class="title">df</span>['<span class="title">Age</span>']获取的么，可以想象这个位置应该是填写列名才对，要不然不是乱套了嘛</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">df</span>[100]         # 错误的方式</span></span><br><span class="line"><span class="class">出错啦</span></span><br><span class="line"><span class="class"># 这种方式返回的也是一个<span class="title">Series</span>结构，<span class="title">Series</span>结构可以表示<span class="title">DataFrame</span>的一行或一列</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">df</span>.<span class="title">iloc</span>[100]</span></span><br><span class="line"><span class="class"><span class="title">PassengerId</span>                        101</span></span><br><span class="line"><span class="class"><span class="title">Survived</span>                             0</span></span><br><span class="line"><span class="class"><span class="title">Pclass</span>                               3</span></span><br><span class="line">Name           Petranec, Miss. Matilda</span><br><span class="line">Sex                             female</span><br><span class="line">Age                                 <span class="number">28</span></span><br><span class="line">SibSp                                <span class="number">0</span></span><br><span class="line">Parch                                <span class="number">0</span></span><br><span class="line">Ticket                          <span class="number">349245</span></span><br><span class="line">Fare                            <span class="number">7.8958</span></span><br><span class="line">Cabin                              NaN</span><br><span class="line">Embarked                             S</span><br><span class="line">Name: <span class="number">100</span>, dtype: object</span><br><span class="line"><span class="comment"># 访问索引为1，列索引为3的数据，可以看到正式Name列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.iloc[<span class="number">1</span>, <span class="number">3</span>]</span><br><span class="line"><span class="string">'Cumings, Mrs. John Bradley (Florence Briggs Thayer)'</span></span><br><span class="line"><span class="comment"># 这两个位置的索引依然支持切片</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.iloc[<span class="number">0</span>:<span class="number">3</span>, <span class="number">2</span>:<span class="number">5</span>]</span><br><span class="line">   Pclass                                               Name     Sex</span><br><span class="line"><span class="number">0</span>       <span class="number">3</span>                            Braund, Mr. Owen Harris    male</span><br><span class="line"><span class="number">1</span>       <span class="number">1</span>  Cumings, Mrs. John Bradley (Florence Briggs Th...  female</span><br><span class="line"><span class="number">2</span>       <span class="number">3</span>                             Heikkinen, Miss. Laina  female</span><br><span class="line"><span class="comment"># 只想选第二列与第四列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.iloc[<span class="number">0</span>:<span class="number">3</span>, [<span class="number">2</span>,<span class="number">4</span>]]</span><br><span class="line">   Pclass     Sex</span><br><span class="line"><span class="number">0</span>       <span class="number">3</span>    male</span><br><span class="line"><span class="number">1</span>       <span class="number">1</span>  female</span><br><span class="line"><span class="number">2</span>       <span class="number">3</span>  female</span><br><span class="line"><span class="comment"># 有时使用列名来选择可能更加直观一些</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="number">0</span>:<span class="number">3</span>][[<span class="string">'Age'</span>, <span class="string">'Fare'</span>]]</span><br><span class="line">    Age     Fare</span><br><span class="line"><span class="number">0</span>  <span class="number">22.0</span>   <span class="number">7.2500</span></span><br><span class="line"><span class="number">1</span>  <span class="number">38.0</span>  <span class="number">71.2833</span></span><br><span class="line"><span class="number">2</span>  <span class="number">26.0</span>   <span class="number">7.9250</span></span><br><span class="line"><span class="comment"># 不指定行索引，按列名选择数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[[<span class="string">'Age'</span>, <span class="string">'Fare'</span>]]</span><br><span class="line">内容太多啦，不贴啦</span><br><span class="line"><span class="comment"># 可以看到通过这种方式选择出来的是一个DataFrame，能看出与 df['Age'] 的区别么</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>type(df[[<span class="string">'Age'</span>, <span class="string">'Fare'</span>]])</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> '<span class="title">pandas</span>.<span class="title">core</span>.<span class="title">frame</span>.<span class="title">DataFrame</span>'&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"># 目前位置我们讨论的索引都是系统默认生成的序号，其实我们是可以按指定列做为索引的，在时间处理中尤为有用</span></span><br><span class="line">&gt;&gt;&gt; newdf = df.set_index('Name')</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newdf.loc[<span class="string">'Heikkinen, Miss. Laina'</span>]</span><br><span class="line">PassengerId                   <span class="number">3</span></span><br><span class="line">Survived                      <span class="number">1</span></span><br><span class="line">Pclass                        <span class="number">3</span></span><br><span class="line">Sex                      female</span><br><span class="line">Age                          <span class="number">26</span></span><br><span class="line">SibSp                         <span class="number">0</span></span><br><span class="line">Parch                         <span class="number">0</span></span><br><span class="line">Ticket         STON/O2. <span class="number">3101282</span></span><br><span class="line">Fare                      <span class="number">7.925</span></span><br><span class="line">Cabin                       NaN</span><br><span class="line">Embarked                      S</span><br><span class="line">Name: Heikkinen, Miss. Laina, dtype: object</span><br><span class="line"><span class="comment"># 或者取这个人的年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newdf.loc[<span class="string">'Heikkinen, Miss. Laina'</span>, <span class="string">'Age'</span>]</span><br><span class="line"><span class="number">26.0</span></span><br><span class="line"><span class="comment"># 取这个人的多个指标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newdf.loc[<span class="string">'Heikkinen, Miss. Laina'</span>, [<span class="string">'Age'</span>, <span class="string">'Sex'</span>]]</span><br><span class="line">Age        <span class="number">26</span></span><br><span class="line">Sex    female</span><br><span class="line">Name: Heikkinen, Miss. Laina, dtype: object</span><br><span class="line"><span class="comment"># 甚至姓名也能使用切片，后面会讲时间相关的，会看到其强大的用途</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newdf.loc[<span class="string">'Heikkinen, Miss. Laina'</span>:<span class="string">'Allen, Mr. William Henry'</span>, [<span class="string">'Age'</span>, <span class="string">'Sex'</span>]]</span><br><span class="line">                                               Age     Sex</span><br><span class="line">Name</span><br><span class="line">Heikkinen, Miss. Laina                        <span class="number">26.0</span>  female</span><br><span class="line">Futrelle, Mrs. Jacques Heath (Lily May Peel)  <span class="number">35.0</span>  female</span><br><span class="line">Allen, Mr. William Henry                      <span class="number">35.0</span>    male</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="关于Series的基本操作"><a href="#关于Series的基本操作" class="headerlink" title="关于Series的基本操作"></a>关于Series的基本操作</h3><p>Series类似ndarray结构是通过数组创建<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(data=data, index=index)</span><br><span class="line"><span class="comment"># 可以看到Series并没列名信息，因为就一列，第一列是索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">a    <span class="number">10</span></span><br><span class="line">b    <span class="number">11</span></span><br><span class="line">c    <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 我们依然能用head/tail方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.head(<span class="number">1</span>)</span><br><span class="line">a    <span class="number">10</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.tail(<span class="number">1</span>)</span><br><span class="line">c    <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 获取ndarray数组</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.values</span><br><span class="line">array([<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>])</span><br><span class="line"><span class="comment"># 也能获取元素类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.dtypes</span><br><span class="line">dtype(<span class="string">'int64'</span>)</span><br><span class="line"><span class="comment"># 获取Series数据集中不重复的元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;</span><br><span class="line"><span class="meta">... </span>    <span class="string">'product'</span>: [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'amount'</span>: [<span class="number">0</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">20</span>]</span><br><span class="line"><span class="meta">... </span>&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'product'</span>].unique()</span><br><span class="line">array([<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>], dtype=object)</span><br></pre></td></tr></table></figure></p>
<p>Series结构也支持一系列的数值运算</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age = df[<span class="string">'Age'</span>]</span><br><span class="line"><span class="comment"># 我们计算平均年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.mean()</span><br><span class="line"><span class="number">29.69911764705882</span></span><br><span class="line"><span class="comment"># 以及最大最小值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.min()</span><br><span class="line"><span class="number">0.42</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.max()</span><br><span class="line"><span class="number">80.0</span></span><br><span class="line"><span class="comment"># 其他等等</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.std()</span><br><span class="line"><span class="number">14.526497332334044</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.var()</span><br><span class="line"><span class="number">211.0191247463081</span></span><br><span class="line"><span class="comment"># 通过describe查看各种统计数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age.describe()</span><br><span class="line">count    <span class="number">714.000000</span></span><br><span class="line">mean      <span class="number">29.699118</span></span><br><span class="line">std       <span class="number">14.526497</span></span><br><span class="line">min        <span class="number">0.420000</span></span><br><span class="line"><span class="number">25</span>%       <span class="number">20.125000</span></span><br><span class="line"><span class="number">50</span>%       <span class="number">28.000000</span></span><br><span class="line"><span class="number">75</span>%       <span class="number">38.000000</span></span><br><span class="line">max       <span class="number">80.000000</span></span><br><span class="line">Name: Age, dtype: float64</span><br></pre></td></tr></table></figure>
<h2 id="进阶"><a href="#进阶" class="headerlink" title="进阶"></a>进阶</h2><p>基础篇的例子都看懂了，我们就可以进行高级一点的内容了</p>
<h3 id="数据的筛选"><a href="#数据的筛选" class="headerlink" title="数据的筛选"></a>数据的筛选</h3><p>基础篇我们介绍了如果去获取数据，这里我们讲讲如何去筛选各种数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 筛选出年龄大于73岁的人</span></span><br><span class="line"><span class="comment"># 这里的flag是一个Series结构，索引就是原始df的索引，列数据为bool类型的值，指明了年龄是否是大于74岁的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>flag = df[<span class="string">'Age'</span>] &gt; <span class="number">73</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[flag]    <span class="comment"># 等价于 df.loc[flag]</span></span><br><span class="line">     PassengerId  Survived  Pclass                                  Name   Sex   Age  SibSp  Parch  Ticket    Fare Cabin Embarked</span><br><span class="line"><span class="number">630</span>          <span class="number">631</span>         <span class="number">1</span>       <span class="number">1</span>  Barkworth, Mr. Algernon Henry Wilson  male  <span class="number">80.0</span>      <span class="number">0</span>      <span class="number">0</span>   <span class="number">27042</span>  <span class="number">30.000</span>   A23        S</span><br><span class="line"><span class="number">851</span>          <span class="number">852</span>         <span class="number">0</span>       <span class="number">3</span>                   Svensson, Mr. Johan  male  <span class="number">74.0</span>      <span class="number">0</span>      <span class="number">0</span>  <span class="number">347060</span>   <span class="number">7.775</span>   NaN        S</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选择性别都是男性的记录</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[df[<span class="string">'Sex'</span>] == <span class="string">'male'</span>].head(<span class="number">1</span>)</span><br><span class="line">   PassengerId  Survived  Pclass                     Name   Sex   Age  SibSp  Parch     Ticket  Fare Cabin Embarked</span><br><span class="line"><span class="number">0</span>            <span class="number">1</span>         <span class="number">0</span>       <span class="number">3</span>  Braund, Mr. Owen Harris  male  <span class="number">22.0</span>      <span class="number">1</span>      <span class="number">0</span>  A/<span class="number">5</span> <span class="number">21171</span>  <span class="number">7.25</span>   NaN        S</span><br><span class="line"></span><br><span class="line"><span class="comment"># 结合我们在基础篇讲的获取数据的内容，这里求所有男性的平均年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[df[<span class="string">'Sex'</span>] == <span class="string">'male'</span>, <span class="string">'Age'</span>].mean()</span><br><span class="line"><span class="number">30.72664459161148</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 求年龄大于70的人数</span></span><br><span class="line">(df[<span class="string">'Age'</span>] &gt; <span class="number">70</span>).sum()          </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>(df[<span class="string">'Age'</span>] &gt; <span class="number">70</span>).sum()</span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="comment"># 是否有考虑过这种求法，想一下为什么是这个结果</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[df[<span class="string">'Age'</span>] &gt; <span class="number">70</span>].sum()</span><br><span class="line">PassengerId                                                 <span class="number">2191</span></span><br><span class="line">Survived                                                       <span class="number">1</span></span><br><span class="line">Pclass                                                         <span class="number">9</span></span><br><span class="line">Name           Goldschmidt, Mr. George BConnors, Mr. PatrickA...</span><br><span class="line">Sex                                         malemalemalemalemale</span><br><span class="line">Age                                                        <span class="number">366.5</span></span><br><span class="line">SibSp                                                          <span class="number">0</span></span><br><span class="line">Parch                                                          <span class="number">0</span></span><br><span class="line">Ticket                         PC <span class="number">17754370369</span>PC <span class="number">1760927042347060</span></span><br><span class="line">Fare                                                     <span class="number">129.683</span></span><br><span class="line">Embarked                                                   CQCSS</span><br><span class="line">dtype: object</span><br></pre></td></tr></table></figure>
<h3 id="groupby"><a href="#groupby" class="headerlink" title="groupby"></a>groupby</h3><p>groupby也是一个高频的操作，理解了groupby将会非常有用</p>
<p>groupby: 将样本按照一定规则进行分组，然后得到分组后的统计信息，这里我们举一个简单的销售额的例子，假设有如下数据</p>
<div><div class="fold_hider"><div class="close hider_title">groupby示例</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 这里模拟了拥有三个产品（A、B、C）的销售额数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;</span><br><span class="line"><span class="meta">... </span>    <span class="string">'product'</span>: [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'amount'</span>: [<span class="number">0</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">20</span>]</span><br><span class="line"><span class="meta">... </span>&#125;)</span><br><span class="line"><span class="comment"># 如果不用groupby，直接计算</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> key <span class="keyword">in</span> df[<span class="string">'product'</span>].unique():</span><br><span class="line"><span class="meta">... </span>    print(key, df[df[<span class="string">'product'</span>] == key].sum())</span><br><span class="line">...</span><br><span class="line">A product    AAA</span><br><span class="line">amount      <span class="number">15</span></span><br><span class="line">dtype: object</span><br><span class="line">B product    BBB</span><br><span class="line">amount      <span class="number">30</span></span><br><span class="line">dtype: object</span><br><span class="line">C product    CCC</span><br><span class="line">amount      <span class="number">45</span></span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 如果使用groupby，直接返回一个DataFrame结构，清晰明了</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'product'</span>).sum()</span><br><span class="line">         amount</span><br><span class="line">product</span><br><span class="line">A            <span class="number">15</span></span><br><span class="line">B            <span class="number">30</span></span><br><span class="line">C            <span class="number">45</span></span><br></pre></td></tr></table></figure>

</div></div>
<p>这里介绍groupby的工作流程</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">                split    apply    combine</span><br><span class="line">A 0             A 0               </span><br><span class="line">B 5             A 5</span><br><span class="line">C 10            A 10</span><br><span class="line">A 5     分块     B 5      sum      A 15</span><br><span class="line">B 10   ====&gt;    B 10     ====&gt;    B 30</span><br><span class="line">C 15            B 15              C 45</span><br><span class="line">A 10            C 10</span><br><span class="line">B 15            C 15</span><br><span class="line">c 20            C 20</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">groupby也支持很多的其他聚合方法</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 获取各类指标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'product'</span>).describe()</span><br><span class="line">        amount</span><br><span class="line">         count  mean  std   min   <span class="number">25</span>%   <span class="number">50</span>%   <span class="number">75</span>%   max</span><br><span class="line">product</span><br><span class="line">A          <span class="number">3.0</span>   <span class="number">5.0</span>  <span class="number">5.0</span>   <span class="number">0.0</span>   <span class="number">2.5</span>   <span class="number">5.0</span>   <span class="number">7.5</span>  <span class="number">10.0</span></span><br><span class="line">B          <span class="number">3.0</span>  <span class="number">10.0</span>  <span class="number">5.0</span>   <span class="number">5.0</span>   <span class="number">7.5</span>  <span class="number">10.0</span>  <span class="number">12.5</span>  <span class="number">15.0</span></span><br><span class="line">C          <span class="number">3.0</span>  <span class="number">15.0</span>  <span class="number">5.0</span>  <span class="number">10.0</span>  <span class="number">12.5</span>  <span class="number">15.0</span>  <span class="number">17.5</span>  <span class="number">20.0</span></span><br><span class="line"><span class="comment"># 其他</span></span><br><span class="line"><span class="comment"># df.groupby('product').min()</span></span><br><span class="line"><span class="comment"># df.groupby('product').max()</span></span><br><span class="line"><span class="comment"># df.groupby('product').mean()</span></span><br><span class="line"><span class="comment"># mean求值等价于调用aggregate方法，其他类似</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'product'</span>).aggregate(np.mean)</span><br><span class="line">         amount</span><br><span class="line">product</span><br><span class="line">A             <span class="number">5</span></span><br><span class="line">B            <span class="number">10</span></span><br><span class="line">C            <span class="number">15</span></span><br></pre></td></tr></table></figure>

</div></div>
<div><div class="fold_hider"><div class="close hider_title">使用groupby实战泰坦尼克号的数据</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="comment"># 按Sex列进行分组，然后肯定只有两个组，男性和女性，这是再求对应指标（如Age）或所有指标的各类聚合信息，这里是求平均年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'Sex'</span>)[<span class="string">'Age'</span>].mean()</span><br><span class="line">Sex</span><br><span class="line">female    <span class="number">27.915709</span></span><br><span class="line">male      <span class="number">30.726645</span></span><br><span class="line">Name: Age, dtype: float64</span><br><span class="line"><span class="comment"># 分别计算男性与女性的获救情况</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'Sex'</span>)[<span class="string">'Survived'</span>].mean()</span><br><span class="line">Sex</span><br><span class="line">female    <span class="number">0.742038</span></span><br><span class="line">male      <span class="number">0.188908</span></span><br><span class="line">Name: Survived, dtype: float64</span><br></pre></td></tr></table></figure>

</div></div>
<p>上面我们的例子都是按某一个列进行分组的，其实我们是可以自定义，按多列进行分组</p>
<div><div class="fold_hider"><div class="close hider_title">自定义分组</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;<span class="string">'A'</span> : [<span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'foo'</span>, <span class="string">'foo'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B'</span> : [<span class="string">'one'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>, <span class="string">'two'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'three'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C'</span> : np.random.randn(<span class="number">8</span>),</span><br><span class="line"><span class="meta">... </span>    <span class="string">'D'</span> : np.random.randn(<span class="number">8</span>)&#125;)</span><br><span class="line"><span class="comment"># 按A列进行分组，求计数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby(<span class="string">'A'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.count()</span><br><span class="line">     B  C  D</span><br><span class="line">A</span><br><span class="line">bar  <span class="number">3</span>  <span class="number">3</span>  <span class="number">3</span></span><br><span class="line">foo  <span class="number">5</span>  <span class="number">5</span>  <span class="number">5</span></span><br><span class="line"><span class="comment"># 分组后我们可以只求指定列的统计指标，比如这里获取C指标的总和、均值和标准差</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped[<span class="string">'C'</span>].agg([np.sum, np.mean, np.std])</span><br><span class="line">          sum      mean       std</span><br><span class="line">A</span><br><span class="line">bar <span class="number">-2.505948</span> <span class="number">-0.835316</span>  <span class="number">0.222418</span></span><br><span class="line">foo <span class="number">-0.880155</span> <span class="number">-0.176031</span>  <span class="number">0.626238</span></span><br><span class="line"><span class="comment"># 按A和B列进行分组，求计数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.count()</span><br><span class="line">           C  D</span><br><span class="line">A   B</span><br><span class="line">bar one    <span class="number">1</span>  <span class="number">1</span></span><br><span class="line">    three  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line">    two    <span class="number">1</span>  <span class="number">1</span></span><br><span class="line">foo one    <span class="number">2</span>  <span class="number">2</span></span><br><span class="line">    three  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line">    two    <span class="number">2</span>  <span class="number">2</span></span><br><span class="line"><span class="comment"># 我们也可以不要索引，通过as_index设置</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>],as_index=<span class="keyword">False</span>).count()</span><br><span class="line">     A      B  C  D</span><br><span class="line"><span class="number">0</span>  bar    one  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line"><span class="number">1</span>  bar  three  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line"><span class="number">2</span>  bar    two  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line"><span class="number">3</span>  foo    one  <span class="number">2</span>  <span class="number">2</span></span><br><span class="line"><span class="number">4</span>  foo  three  <span class="number">1</span>  <span class="number">1</span></span><br><span class="line"><span class="number">5</span>  foo    two  <span class="number">2</span>  <span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>],as_index=<span class="keyword">False</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.aggregate(np.sum)</span><br><span class="line">     A      B         C         D</span><br><span class="line"><span class="number">0</span>  bar    one <span class="number">-0.928055</span>  <span class="number">0.247315</span></span><br><span class="line"><span class="number">1</span>  bar  three <span class="number">-0.996357</span>  <span class="number">0.455334</span></span><br><span class="line"><span class="number">2</span>  bar    two <span class="number">-0.581536</span>  <span class="number">0.981273</span></span><br><span class="line"><span class="number">3</span>  foo    one <span class="number">-0.376836</span>  <span class="number">1.330245</span></span><br><span class="line"><span class="number">4</span>  foo  three  <span class="number">0.449950</span> <span class="number">-1.810325</span></span><br><span class="line"><span class="number">5</span>  foo    two <span class="number">-0.953268</span> <span class="number">-1.891668</span></span><br><span class="line"><span class="comment"># 与上例等价</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>]).sum().reset_index()</span><br><span class="line">与上面一致，不贴了</span><br><span class="line"><span class="comment"># 也能使用describe</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby([<span class="string">'A'</span>, <span class="string">'B'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.describe()</span><br><span class="line">              C                                                                           D</span><br><span class="line">          count      mean       std       min       <span class="number">25</span>%       <span class="number">50</span>%       <span class="number">75</span>%       max count      mean       std       min       <span class="number">25</span>%       <span class="number">50</span>%       <span class="number">75</span>%       max</span><br><span class="line">A   B</span><br><span class="line">bar one     <span class="number">1.0</span> <span class="number">-0.928055</span>       NaN <span class="number">-0.928055</span> <span class="number">-0.928055</span> <span class="number">-0.928055</span> <span class="number">-0.928055</span> <span class="number">-0.928055</span>   <span class="number">1.0</span>  <span class="number">0.247315</span>       NaN  <span class="number">0.247315</span>  <span class="number">0.247315</span>  <span class="number">0.247315</span>  <span class="number">0.247315</span>  <span class="number">0.247315</span></span><br><span class="line">    three   <span class="number">1.0</span> <span class="number">-0.996357</span>       NaN <span class="number">-0.996357</span> <span class="number">-0.996357</span> <span class="number">-0.996357</span> <span class="number">-0.996357</span> <span class="number">-0.996357</span>   <span class="number">1.0</span>  <span class="number">0.455334</span>       NaN  <span class="number">0.455334</span>  <span class="number">0.455334</span>  <span class="number">0.455334</span>  <span class="number">0.455334</span>  <span class="number">0.455334</span></span><br><span class="line">    two     <span class="number">1.0</span> <span class="number">-0.581536</span>       NaN <span class="number">-0.581536</span> <span class="number">-0.581536</span> <span class="number">-0.581536</span> <span class="number">-0.581536</span> <span class="number">-0.581536</span>   <span class="number">1.0</span>  <span class="number">0.981273</span>       NaN  <span class="number">0.981273</span>  <span class="number">0.981273</span>  <span class="number">0.981273</span>  <span class="number">0.981273</span>  <span class="number">0.981273</span></span><br><span class="line">foo one     <span class="number">2.0</span> <span class="number">-0.188418</span>  <span class="number">0.644285</span> <span class="number">-0.643996</span> <span class="number">-0.416207</span> <span class="number">-0.188418</span>  <span class="number">0.039371</span>  <span class="number">0.267160</span>   <span class="number">2.0</span>  <span class="number">0.665123</span>  <span class="number">2.384508</span> <span class="number">-1.020979</span> <span class="number">-0.177928</span>  <span class="number">0.665123</span>  <span class="number">1.508173</span>  <span class="number">2.351224</span></span><br><span class="line">    three   <span class="number">1.0</span>  <span class="number">0.449950</span>       NaN  <span class="number">0.449950</span>  <span class="number">0.449950</span>  <span class="number">0.449950</span>  <span class="number">0.449950</span>  <span class="number">0.449950</span>   <span class="number">1.0</span> <span class="number">-1.810325</span>       NaN <span class="number">-1.810325</span> <span class="number">-1.810325</span> <span class="number">-1.810325</span> <span class="number">-1.810325</span> <span class="number">-1.810325</span></span><br><span class="line">    two     <span class="number">2.0</span> <span class="number">-0.476634</span>  <span class="number">0.762045</span> <span class="number">-1.015481</span> <span class="number">-0.746058</span> <span class="number">-0.476634</span> <span class="number">-0.207210</span>  <span class="number">0.062213</span>   <span class="number">2.0</span> <span class="number">-0.945834</span>  <span class="number">0.804517</span> <span class="number">-1.514713</span> <span class="number">-1.230274</span> <span class="number">-0.945834</span> <span class="number">-0.661394</span> <span class="number">-0.376954</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 除了使用列作为分组的条件外，我们也可以指定一个函数来设置分组的规则</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">get_letter_type</span><span class="params">(letter)</span>:</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">if</span> letter.lower() <span class="keyword">in</span> <span class="string">'aeiou'</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">'a'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">else</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">'b'</span></span><br><span class="line">...</span><br><span class="line"><span class="comment"># 制定 get_letter_type 函数为分组方式</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped = df.groupby(get_letter_type, axis=<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>grouped.count()</span><br><span class="line">   a  b</span><br><span class="line"><span class="number">0</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">2</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">3</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">4</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">5</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">6</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="number">7</span>  <span class="number">1</span>  <span class="number">3</span></span><br><span class="line"><span class="comment"># 为了加深理解，我们在上面的例子基础上再给你一个例子，我们要按索引是否大于5位分组条件，来计算累计和</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">get_range_type</span><span class="params">(letter)</span>:</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">if</span> letter &gt; <span class="number">5</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">'yes'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">else</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">'no'</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(get_range_type, axis=<span class="number">0</span>).sum()</span><br><span class="line">            C         D</span><br><span class="line">no  <span class="number">-4.103212</span> <span class="number">-1.228725</span></span><br><span class="line">yes  <span class="number">0.717109</span>  <span class="number">0.540899</span></span><br></pre></td></tr></table></figure>

</div></div>
<p>分组后我们可能只想关注某一个分组<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;<span class="string">'X'</span>: [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'A'</span>, <span class="string">'B'</span>], <span class="string">'Y'</span>: [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">   X  Y</span><br><span class="line"><span class="number">0</span>  A  <span class="number">1</span></span><br><span class="line"><span class="number">1</span>  B  <span class="number">2</span></span><br><span class="line"><span class="number">2</span>  A  <span class="number">3</span></span><br><span class="line"><span class="number">3</span>  B  <span class="number">4</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.groupby(<span class="string">'X'</span>).get_group(<span class="string">'A'</span>)</span><br><span class="line">   X  Y</span><br><span class="line"><span class="number">0</span>  A  <span class="number">1</span></span><br><span class="line"><span class="number">2</span>  A  <span class="number">3</span></span><br></pre></td></tr></table></figure></p>
<p>多重索引下，我们可以制定level操作，level为0表示第一个索引，level为1表示第二个索引。。。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arrays = [[<span class="string">'bar'</span>, <span class="string">'bar'</span>, <span class="string">'baz'</span>, <span class="string">'baz'</span>, <span class="string">'foo'</span>, <span class="string">'foo'</span>, <span class="string">'qux'</span>, <span class="string">'qux'</span>],</span><br><span class="line"><span class="meta">... </span>    [<span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = pd.MultiIndex.from_arrays(arrays, names=[<span class="string">'first'</span>, <span class="string">'second'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(np.random.randn(<span class="number">8</span>),index=index)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">first  second</span><br><span class="line">bar    one       <span class="number">0.383993</span></span><br><span class="line">       two      <span class="number">-3.055530</span></span><br><span class="line">baz    one      <span class="number">-0.831237</span></span><br><span class="line">       two      <span class="number">-1.015493</span></span><br><span class="line">foo    one      <span class="number">-0.234695</span></span><br><span class="line">       two      <span class="number">-1.641438</span></span><br><span class="line">qux    one      <span class="number">-0.462693</span></span><br><span class="line">       two      <span class="number">-1.568615</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># 我们按第一个索引进行求和</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.groupby(level=<span class="number">0</span>).sum()    <span class="comment"># 等价于 s.groupby(level="first").sum()</span></span><br><span class="line">first</span><br><span class="line">bar   <span class="number">-2.671537</span></span><br><span class="line">baz   <span class="number">-1.846730</span></span><br><span class="line">foo   <span class="number">-1.876133</span></span><br><span class="line">qux   <span class="number">-2.031308</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># 也可以按第二个索引进行求和</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.groupby(level=<span class="number">1</span>).sum()    <span class="comment"># 等价于 s.groupby(level="second").sum()</span></span><br><span class="line">second</span><br><span class="line">one   <span class="number">-1.144633</span></span><br><span class="line">two   <span class="number">-7.281076</span></span><br><span class="line">dtype: float64</span><br></pre></td></tr></table></figure>
<h3 id="数值运算"><a href="#数值运算" class="headerlink" title="数值运算"></a>数值运算</h3><p>在讲解numpy那篇文章，我们讲解了其数值运算，pandas的数值运算同样强大</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 注意这里的index参数，默认我们的索引都是从0开始递增，有多少个记录索引就依次递增多少</span></span><br><span class="line"><span class="comment"># 除了使用默认的递增索引，我们也可以自己指定，还记得泰坦尼克号的数据，我们以名字作为索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame([[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],[<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]], index=[<span class="string">'a'</span>, <span class="string">'b'</span>], columns=[<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">   A  B  C</span><br><span class="line">a  <span class="number">1</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">b  <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 求和。对于DataFrame结构，其是一个table的结构，自然就有两个维度，横轴和纵轴，在求和的时候也需要注意是按哪个轴进行求和</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.sum()    <span class="comment"># 等价于 df.sum(axis=0) df.sum(axis='rows')</span></span><br><span class="line">A    <span class="number">5</span></span><br><span class="line">B    <span class="number">7</span></span><br><span class="line">C    <span class="number">9</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.sum(axis=<span class="number">1</span>)  <span class="comment"># 等价于 df.sum(axis='columns')</span></span><br><span class="line">a     <span class="number">6</span></span><br><span class="line">b    <span class="number">15</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 其他方法，同样需要指定轴</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.min()</span><br><span class="line">A    <span class="number">1</span></span><br><span class="line">B    <span class="number">2</span></span><br><span class="line">C    <span class="number">3</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.max()</span><br><span class="line">A    <span class="number">4</span></span><br><span class="line">B    <span class="number">5</span></span><br><span class="line">C    <span class="number">6</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.median()</span><br><span class="line">A    <span class="number">2.5</span></span><br><span class="line">B    <span class="number">3.5</span></span><br><span class="line">C    <span class="number">4.5</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.mean()</span><br><span class="line">A    <span class="number">2.5</span></span><br><span class="line">B    <span class="number">3.5</span></span><br><span class="line">C    <span class="number">4.5</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># 我们这里还是引入泰坦尼克号的数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="comment"># 我们要统计某一个指标的样本值的计数，默认是降序，可以通过 scending=True 指定为升序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Age'</span>].value_counts().head()</span><br><span class="line"><span class="number">24.0</span>    <span class="number">30</span></span><br><span class="line"><span class="number">22.0</span>    <span class="number">27</span></span><br><span class="line"><span class="number">18.0</span>    <span class="number">26</span></span><br><span class="line"><span class="number">19.0</span>    <span class="number">25</span></span><br><span class="line"><span class="number">30.0</span>    <span class="number">25</span></span><br><span class="line">Name: Age, dtype: int64</span><br><span class="line"><span class="comment"># 我们也可以指定区间范围，按范围计数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Age'</span>].value_counts(bins=<span class="number">5</span>)</span><br><span class="line">(<span class="number">16.336</span>, <span class="number">32.252</span>]    <span class="number">346</span></span><br><span class="line">(<span class="number">32.252</span>, <span class="number">48.168</span>]    <span class="number">188</span></span><br><span class="line">(<span class="number">0.339</span>, <span class="number">16.336</span>]     <span class="number">100</span></span><br><span class="line">(<span class="number">48.168</span>, <span class="number">64.084</span>]     <span class="number">69</span></span><br><span class="line">(<span class="number">64.084</span>, <span class="number">80.0</span>]       <span class="number">11</span></span><br><span class="line">Name: Age, dtype: int64</span><br><span class="line"><span class="comment"># 再来一个好理解的，比如要统计所有人中男女的人数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Sex'</span>].value_counts()</span><br><span class="line">male      <span class="number">577</span></span><br><span class="line">female    <span class="number">314</span></span><br><span class="line">Name: Sex, dtype: int64</span><br><span class="line"><span class="comment"># 如果要获取对应指标的样本数</span></span><br><span class="line"><span class="comment"># 本来这两个值都应该一样才对，但如果某一个指标存在缺失值，那么对应指标样本数就会变少</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Age'</span>].count()</span><br><span class="line"><span class="number">714</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Pclass'</span>].count()</span><br><span class="line"><span class="number">891</span></span><br></pre></td></tr></table></figure>
<p>除了单个指标的计算，我们也可以进行二元统计，比如计算<a href="https://www.cnblogs.com/tsingke/p/6273970.html">协方差</a>、<a href="https://www.cnblogs.com/sanshanyin/p/5397091.html">相关系数</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.cov()</span><br><span class="line">              PassengerId  Survived     Pclass         Age      SibSp     Parch         Fare</span><br><span class="line">PassengerId  <span class="number">66231.000000</span> <span class="number">-0.626966</span>  <span class="number">-7.561798</span>  <span class="number">138.696504</span> <span class="number">-16.325843</span> <span class="number">-0.342697</span>   <span class="number">161.883369</span></span><br><span class="line">Survived        <span class="number">-0.626966</span>  <span class="number">0.236772</span>  <span class="number">-0.137703</span>   <span class="number">-0.551296</span>  <span class="number">-0.018954</span>  <span class="number">0.032017</span>     <span class="number">6.221787</span></span><br><span class="line">Pclass          <span class="number">-7.561798</span> <span class="number">-0.137703</span>   <span class="number">0.699015</span>   <span class="number">-4.496004</span>   <span class="number">0.076599</span>  <span class="number">0.012429</span>   <span class="number">-22.830196</span></span><br><span class="line">Age            <span class="number">138.696504</span> <span class="number">-0.551296</span>  <span class="number">-4.496004</span>  <span class="number">211.019125</span>  <span class="number">-4.163334</span> <span class="number">-2.344191</span>    <span class="number">73.849030</span></span><br><span class="line">SibSp          <span class="number">-16.325843</span> <span class="number">-0.018954</span>   <span class="number">0.076599</span>   <span class="number">-4.163334</span>   <span class="number">1.216043</span>  <span class="number">0.368739</span>     <span class="number">8.748734</span></span><br><span class="line">Parch           <span class="number">-0.342697</span>  <span class="number">0.032017</span>   <span class="number">0.012429</span>   <span class="number">-2.344191</span>   <span class="number">0.368739</span>  <span class="number">0.649728</span>     <span class="number">8.661052</span></span><br><span class="line">Fare           <span class="number">161.883369</span>  <span class="number">6.221787</span> <span class="number">-22.830196</span>   <span class="number">73.849030</span>   <span class="number">8.748734</span>  <span class="number">8.661052</span>  <span class="number">2469.436846</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.corr()</span><br><span class="line">             PassengerId  Survived    Pclass       Age     SibSp     Parch      Fare</span><br><span class="line">PassengerId     <span class="number">1.000000</span> <span class="number">-0.005007</span> <span class="number">-0.035144</span>  <span class="number">0.036847</span> <span class="number">-0.057527</span> <span class="number">-0.001652</span>  <span class="number">0.012658</span></span><br><span class="line">Survived       <span class="number">-0.005007</span>  <span class="number">1.000000</span> <span class="number">-0.338481</span> <span class="number">-0.077221</span> <span class="number">-0.035322</span>  <span class="number">0.081629</span>  <span class="number">0.257307</span></span><br><span class="line">Pclass         <span class="number">-0.035144</span> <span class="number">-0.338481</span>  <span class="number">1.000000</span> <span class="number">-0.369226</span>  <span class="number">0.083081</span>  <span class="number">0.018443</span> <span class="number">-0.549500</span></span><br><span class="line">Age             <span class="number">0.036847</span> <span class="number">-0.077221</span> <span class="number">-0.369226</span>  <span class="number">1.000000</span> <span class="number">-0.308247</span> <span class="number">-0.189119</span>  <span class="number">0.096067</span></span><br><span class="line">SibSp          <span class="number">-0.057527</span> <span class="number">-0.035322</span>  <span class="number">0.083081</span> <span class="number">-0.308247</span>  <span class="number">1.000000</span>  <span class="number">0.414838</span>  <span class="number">0.159651</span></span><br><span class="line">Parch          <span class="number">-0.001652</span>  <span class="number">0.081629</span>  <span class="number">0.018443</span> <span class="number">-0.189119</span>  <span class="number">0.414838</span>  <span class="number">1.000000</span>  <span class="number">0.216225</span></span><br><span class="line">Fare            <span class="number">0.012658</span>  <span class="number">0.257307</span> <span class="number">-0.549500</span>  <span class="number">0.096067</span>  <span class="number">0.159651</span>  <span class="number">0.216225</span>  <span class="number">1.000000</span></span><br></pre></td></tr></table></figure>
<h3 id="DataFrame与Series对象的操作"><a href="#DataFrame与Series对象的操作" class="headerlink" title="DataFrame与Series对象的操作"></a>DataFrame与Series对象的操作</h3><p>前面讲了DataFrame和Series结构的一些常用操作，这里我们再讲一些其他的操作</p>
<div><div class="fold_hider"><div class="close hider_title">Series结构的其他常用操作</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们可以直接修改Series结构的值及索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = [<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(data=data, index=index)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">a    <span class="number">10</span></span><br><span class="line">b    <span class="number">11</span></span><br><span class="line">c    <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 拷贝一个DataFrame</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1 = s.copy()</span><br><span class="line"><span class="comment"># 这两种方式都可以定位到某一个元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1[<span class="string">'a'</span>] = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1[<span class="number">1</span>] = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">a    <span class="number">100</span></span><br><span class="line">b    <span class="number">100</span></span><br><span class="line">c     <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 除了修改某一个元素，也可以修改某一堆元素，inplace表示知否修改原始数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.replace(to_replace=<span class="number">100</span>, value=<span class="number">101</span>, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">a    <span class="number">101</span></span><br><span class="line">b    <span class="number">101</span></span><br><span class="line">c     <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 除了修改值，也可以修改索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.index = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'d'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">a    <span class="number">101</span></span><br><span class="line">b    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 上面修改索引是全部修改，我们也可以只修改部分的索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.rename(index=&#123;<span class="string">'a'</span>:<span class="string">'A'</span>, <span class="string">'b'</span>:<span class="string">'B'</span>&#125;, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">A    <span class="number">101</span></span><br><span class="line">B    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 除了删除、修改，我们也可以直接增加</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2 = pd.Series([<span class="number">100</span>,<span class="number">500</span>], index=[<span class="string">'g'</span>, <span class="string">'h'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2</span><br><span class="line">g    <span class="number">100</span></span><br><span class="line">h    <span class="number">500</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 添加内容</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.append(s2, ignore_index=<span class="keyword">False</span>)</span><br><span class="line">A    <span class="number">101</span></span><br><span class="line">B    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">g    <span class="number">100</span></span><br><span class="line">h    <span class="number">500</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 直接使用新的索引增加，就像使用字典一样</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1[<span class="string">'k'</span>] = <span class="number">110</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">A    <span class="number">101</span></span><br><span class="line">B    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">k    <span class="number">110</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 删除某一个记录</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> s1[<span class="string">'A'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">B    <span class="number">101</span></span><br><span class="line">d     <span class="number">12</span></span><br><span class="line">k    <span class="number">110</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 删除多行</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1.drop([<span class="string">'B'</span>, <span class="string">'d'</span>], inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1</span><br><span class="line">k    <span class="number">110</span></span><br><span class="line">dtype: int64</span><br></pre></td></tr></table></figure>

</div></div>
<div><div class="fold_hider"><div class="close hider_title">DataFrame的常用操作</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = [[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],[<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index=[<span class="string">'a'</span>, <span class="string">'b'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>columns = [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(data=data, index=index, columns=columns)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">   A  B  C</span><br><span class="line">a  <span class="number">1</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">b  <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 查询某一个指标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'A'</span>]</span><br><span class="line">a    <span class="number">1</span></span><br><span class="line">b    <span class="number">4</span></span><br><span class="line">Name: A, dtype: int64</span><br><span class="line"><span class="comment"># 查询某一行</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.iloc[<span class="number">0</span>]      <span class="comment"># 等价于 df.loc['a']</span></span><br><span class="line">A    <span class="number">1</span></span><br><span class="line">B    <span class="number">2</span></span><br><span class="line">C    <span class="number">3</span></span><br><span class="line">Name: a, dtype: int64</span><br><span class="line"><span class="comment"># 要修改某一个值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[<span class="string">'a'</span>][<span class="string">'A'</span>] = <span class="number">150</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C</span><br><span class="line">a  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">b    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 同样可以修改索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.index = [<span class="string">'f'</span>, <span class="string">'g'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 增加一行</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[<span class="string">'c'</span>] = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line">c    <span class="number">1</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line"><span class="comment"># 增加一列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'D'</span>] = [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C   D</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span>  <span class="number">10</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span>  <span class="number">11</span></span><br><span class="line">c    <span class="number">1</span>  <span class="number">2</span>  <span class="number">3</span>  <span class="number">12</span></span><br><span class="line"><span class="comment"># 删除索引为c的记录</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.drop([<span class="string">'c'</span>], axis=<span class="number">0</span>, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C   D</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span>  <span class="number">10</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span>  <span class="number">11</span></span><br><span class="line"><span class="comment"># 删除D这一列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> df[<span class="string">'D'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A  B  C</span><br><span class="line">f  <span class="number">150</span>  <span class="number">2</span>  <span class="number">3</span></span><br><span class="line">g    <span class="number">4</span>  <span class="number">5</span>  <span class="number">6</span></span><br><span class="line"><span class="comment"># 如果要删除多列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.drop([<span class="string">'B'</span>, <span class="string">'C'</span>], axis=<span class="number">1</span>, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">     A</span><br><span class="line">f  <span class="number">150</span></span><br><span class="line">g    <span class="number">4</span></span><br><span class="line"><span class="comment"># DataFrame同样支持合并</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left = pd.DataFrame(&#123;<span class="string">'key'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'A'</span>: [<span class="string">'A0'</span>, <span class="string">'A1'</span>, <span class="string">'A2'</span>, <span class="string">'A3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B'</span>: [<span class="string">'B0'</span>, <span class="string">'B1'</span>, <span class="string">'B2'</span>, <span class="string">'B3'</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right = pd.DataFrame(&#123;<span class="string">'key'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C'</span>: [<span class="string">'C0'</span>, <span class="string">'C1'</span>, <span class="string">'C2'</span>, <span class="string">'C3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'D'</span>: [<span class="string">'D0'</span>, <span class="string">'D1'</span>, <span class="string">'D2'</span>, <span class="string">'D3'</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left</span><br><span class="line">  key   A   B</span><br><span class="line"><span class="number">0</span>  K0  A0  B0</span><br><span class="line"><span class="number">1</span>  K1  A1  B1</span><br><span class="line"><span class="number">2</span>  K2  A2  B2</span><br><span class="line"><span class="number">3</span>  K3  A3  B3</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right</span><br><span class="line">  key   C   D</span><br><span class="line"><span class="number">0</span>  K0  C0  D0</span><br><span class="line"><span class="number">1</span>  K1  C1  D1</span><br><span class="line"><span class="number">2</span>  K2  C2  D2</span><br><span class="line"><span class="number">3</span>  K3  C3  D3</span><br><span class="line"><span class="comment"># 这两个DataFrame都有一个指标叫 key，我们可以按这个key进行合并</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left=left, right=right, on=<span class="string">"key"</span>)</span><br><span class="line">  key   A   B   C   D</span><br><span class="line"><span class="number">0</span>  K0  A0  B0  C0  D0</span><br><span class="line"><span class="number">1</span>  K1  A1  B1  C1  D1</span><br><span class="line"><span class="number">2</span>  K2  A2  B2  C2  D2</span><br><span class="line"><span class="number">3</span>  K3  A3  B3  C3  D3</span><br><span class="line"><span class="comment"># 假设现在有两个列相同</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left[<span class="string">'newkey'</span>] = [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right[<span class="string">'newkey'</span>] = [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left</span><br><span class="line">  key   A   B newkey</span><br><span class="line"><span class="number">0</span>  K0  A0  B0     K0</span><br><span class="line"><span class="number">1</span>  K1  A1  B1     K1</span><br><span class="line"><span class="number">2</span>  K2  A2  B2     K2</span><br><span class="line"><span class="number">3</span>  K3  A3  B3     K3</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right</span><br><span class="line">  key   C   D newkey</span><br><span class="line"><span class="number">0</span>  K0  C0  D0     K0</span><br><span class="line"><span class="number">1</span>  K1  C1  D1     K1</span><br><span class="line"><span class="number">2</span>  K2  C2  D2     K2</span><br><span class="line"><span class="number">3</span>  K3  C3  D3     K3</span><br><span class="line"><span class="comment"># 直接按两个列进行合并</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left,right,on=[<span class="string">'key'</span>,<span class="string">'newkey'</span>])</span><br><span class="line">  key   A   B newkey   C   D</span><br><span class="line"><span class="number">0</span>  K0  A0  B0     K0  C0  D0</span><br><span class="line"><span class="number">1</span>  K1  A1  B1     K1  C1  D1</span><br><span class="line"><span class="number">2</span>  K2  A2  B2     K2  C2  D2</span><br><span class="line"><span class="number">3</span>  K3  A3  B3     K3  C3  D3</span><br><span class="line"><span class="comment"># 假设这两列并非完全相同得内容</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left = pd.DataFrame(&#123;<span class="string">'key1'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'key2'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'A'</span>: [<span class="string">'A0'</span>, <span class="string">'A1'</span>, <span class="string">'A2'</span>, <span class="string">'A3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B'</span>: [<span class="string">'B0'</span>, <span class="string">'B1'</span>, <span class="string">'B2'</span>, <span class="string">'B3'</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right = pd.DataFrame(&#123;<span class="string">'key1'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'key2'</span>: [<span class="string">'K0'</span>,<span class="string">'K1'</span>,<span class="string">'K2'</span>,<span class="string">'K4'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C'</span>: [<span class="string">'C0'</span>, <span class="string">'C1'</span>, <span class="string">'C2'</span>, <span class="string">'C3'</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'D'</span>: [<span class="string">'D0'</span>, <span class="string">'D1'</span>, <span class="string">'D2'</span>, <span class="string">'D3'</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>left</span><br><span class="line">  key1 key2   A   B</span><br><span class="line"><span class="number">0</span>   K0   K0  A0  B0</span><br><span class="line"><span class="number">1</span>   K1   K1  A1  B1</span><br><span class="line"><span class="number">2</span>   K2   K2  A2  B2</span><br><span class="line"><span class="number">3</span>   K3   K3  A3  B3</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>right</span><br><span class="line">  key1 key2   C   D</span><br><span class="line"><span class="number">0</span>   K0   K0  C0  D0</span><br><span class="line"><span class="number">1</span>   K1   K1  C1  D1</span><br><span class="line"><span class="number">2</span>   K2   K2  C2  D2</span><br><span class="line"><span class="number">3</span>   K3   K4  C3  D3</span><br><span class="line"><span class="comment"># 不一样的记录将被删除</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left,right,on=[<span class="string">'key1'</span>,<span class="string">'key2'</span>])</span><br><span class="line">  key1 key2   A   B   C   D</span><br><span class="line"><span class="number">0</span>   K0   K0  A0  B0  C0  D0</span><br><span class="line"><span class="number">1</span>   K1   K1  A1  B1  C1  D1</span><br><span class="line"><span class="number">2</span>   K2   K2  A2  B2  C2  D2</span><br><span class="line"><span class="comment"># 如果不想删除</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left=left, right=right, on=[<span class="string">'key1'</span>,<span class="string">'key2'</span>],how=<span class="string">'outer'</span>)</span><br><span class="line">  key1 key2    A    B    C    D</span><br><span class="line"><span class="number">0</span>   K0   K0   A0   B0   C0   D0</span><br><span class="line"><span class="number">1</span>   K1   K1   A1   B1   C1   D1</span><br><span class="line"><span class="number">2</span>   K2   K2   A2   B2   C2   D2</span><br><span class="line"><span class="number">3</span>   K3   K3   A3   B3  NaN  NaN</span><br><span class="line"><span class="number">4</span>   K3   K4  NaN  NaN   C3   D3</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.merge(left=left, right=right, on=[<span class="string">'key1'</span>,<span class="string">'key2'</span>],how=<span class="string">'outer'</span>,indicator=<span class="keyword">True</span>)</span><br><span class="line">  key1 key2    A    B    C    D      _merge</span><br><span class="line"><span class="number">0</span>   K0   K0   A0   B0   C0   D0        both</span><br><span class="line"><span class="number">1</span>   K1   K1   A1   B1   C1   D1        both</span><br><span class="line"><span class="number">2</span>   K2   K2   A2   B2   C2   D2        both</span><br><span class="line"><span class="number">3</span>   K3   K3   A3   B3  NaN  NaN   left_only</span><br><span class="line"><span class="number">4</span>   K3   K4  NaN  NaN   C3   D3  right_only</span><br><span class="line"><span class="comment"># 这些合并操作什么时候会用到呢？在分析过程中会分模块去过滤一些数据，最后将这些数据合并在一起</span></span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="关于pandas的选项设置"><a href="#关于pandas的选项设置" class="headerlink" title="关于pandas的选项设置"></a>关于pandas的选项设置</h3><p>通过<code>pd.get_option</code>可以查看选项值，具体可以参考 <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.set_option.html">set_option</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们在打印某个DataFrame的时候，如果数据记录非常多，pandas会隐藏一部分，这个是可以设置最大显示行数的</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.set_option(<span class="string">'display.max_rows'</span>, <span class="number">100</span>)  <span class="comment"># 默认是60</span></span><br><span class="line"><span class="comment"># 同样能够设置显示列的最大值 </span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.set_option(<span class="string">'display.max_columns'</span>, <span class="number">30</span>)</span><br><span class="line"><span class="comment"># 字符串最大显示宽度</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.set_option(<span class="string">'display.max_colwidth'</span>, <span class="number">20</span>)</span><br><span class="line"><span class="comment"># 小数点精度</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.set_option(<span class="string">'display.precision'</span>, <span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<h3 id="pandas对时间的操作"><a href="#pandas对时间的操作" class="headerlink" title="pandas对时间的操作"></a>pandas对时间的操作</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 先看一下原生python代码如何处理时间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> datetime</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dt = datetime.datetime(year=<span class="number">2018</span>,month=<span class="number">9</span>,day=<span class="number">16</span>,hour=<span class="number">21</span>,minute=<span class="number">37</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(dt)</span><br><span class="line"><span class="number">2018</span><span class="number">-09</span><span class="number">-16</span> <span class="number">21</span>:<span class="number">37</span>:<span class="number">00</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dt.year</span><br><span class="line"><span class="number">2018</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dt.month</span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dt.day</span><br><span class="line"><span class="number">16</span></span><br><span class="line"><span class="comment"># pandas对时间的处理</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.Timestamp(<span class="string">'2018-09-16'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts = pd.Timestamp(<span class="string">'2018-09-16'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(ts)</span><br><span class="line"><span class="number">2018</span><span class="number">-09</span><span class="number">-16</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.year</span><br><span class="line"><span class="number">2018</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.month</span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.day</span><br><span class="line"><span class="number">16</span></span><br><span class="line"><span class="comment"># 除了Timestamp函数，还有to_datetime函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.to_datetime(<span class="string">'2018-09-16'</span>)</span><br><span class="line">Timestamp(<span class="string">'2018-09-16 00:00:00'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.to_datetime(<span class="string">'16/09/2018'</span>)</span><br><span class="line">Timestamp(<span class="string">'2018-09-16 00:00:00'</span>)</span><br><span class="line"><span class="comment"># 获取5天以后的时间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts + pd.Timedelta(<span class="string">'5 days'</span>)</span><br><span class="line">Timestamp(<span class="string">'2018-09-21 00:00:00'</span>)</span><br></pre></td></tr></table></figure>
<p>可以利用Series结构构建时间记录</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'2017-11-24 00:00:00'</span>, <span class="string">'2017-11-25 00:00:00'</span>, <span class="string">'2017-11-26 00:00:00'</span>])</span><br><span class="line"><span class="comment"># 这里的数据类型是字符串，所有dtype为object</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line"><span class="number">0</span>    <span class="number">2017</span><span class="number">-11</span><span class="number">-24</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="number">1</span>    <span class="number">2017</span><span class="number">-11</span><span class="number">-25</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="number">2</span>    <span class="number">2017</span><span class="number">-11</span><span class="number">-26</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 转换为datetime类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts = pd.to_datetime(s)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts</span><br><span class="line"><span class="number">0</span>   <span class="number">2017</span><span class="number">-11</span><span class="number">-24</span></span><br><span class="line"><span class="number">1</span>   <span class="number">2017</span><span class="number">-11</span><span class="number">-25</span></span><br><span class="line"><span class="number">2</span>   <span class="number">2017</span><span class="number">-11</span><span class="number">-26</span></span><br><span class="line">dtype: datetime64[ns]</span><br><span class="line"><span class="comment"># 对于这种时间属性，可以通过dt获取，如果没有时间属性，则会报错</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.dt.year</span><br><span class="line"><span class="number">0</span>    <span class="number">2017</span></span><br><span class="line"><span class="number">1</span>    <span class="number">2017</span></span><br><span class="line"><span class="number">2</span>    <span class="number">2017</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ts.dt.weekday</span><br><span class="line"><span class="number">0</span>    <span class="number">4</span></span><br><span class="line"><span class="number">1</span>    <span class="number">5</span></span><br><span class="line"><span class="number">2</span>    <span class="number">6</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 我们可以创建时间序列，start：开始时间，periods：时长，间隔时间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.Series(pd.date_range(start=<span class="string">'2018-09-16'</span>, periods=<span class="number">3</span>, freq=<span class="string">'12H'</span>))</span><br><span class="line"><span class="number">0</span>   <span class="number">2018</span><span class="number">-09</span><span class="number">-16</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="number">1</span>   <span class="number">2018</span><span class="number">-09</span><span class="number">-16</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line"><span class="number">2</span>   <span class="number">2018</span><span class="number">-09</span><span class="number">-17</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span></span><br><span class="line">dtype: datetime64[ns]</span><br></pre></td></tr></table></figure>
<p>我们拿真实的数据试验，使用flowdata数据集</p>
<div><div class="fold_hider"><div class="close hider_title">flowdata试验</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'flowdata.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.head()</span><br><span class="line">                  Time   L06_347  LS06_347  LS06_348</span><br><span class="line"><span class="number">0</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.137417</span>  <span class="number">0.097500</span>  <span class="number">0.016833</span></span><br><span class="line"><span class="number">1</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">03</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.131250</span>  <span class="number">0.088833</span>  <span class="number">0.016417</span></span><br><span class="line"><span class="number">2</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">06</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.113500</span>  <span class="number">0.091250</span>  <span class="number">0.016750</span></span><br><span class="line"><span class="number">3</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.135750</span>  <span class="number">0.091500</span>  <span class="number">0.016250</span></span><br><span class="line"><span class="number">4</span>  <span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.140917</span>  <span class="number">0.096167</span>  <span class="number">0.017000</span></span><br><span class="line"><span class="comment"># 转成时间类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Time'</span>] = pd.to_datetime(df[<span class="string">'Time'</span>])</span><br><span class="line"><span class="comment"># 将Time作为index</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.set_index(<span class="string">'Time'</span>)</span><br><span class="line"><span class="comment"># 上面的代码我们先读取出数据，再将字符串的时间转化为时间格式，再将这个列作为index，其实有更方便得处理方式</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'flowdata.csv'</span>, index_col=<span class="number">0</span>, parse_dates=<span class="keyword">True</span>)</span><br><span class="line"><span class="comment"># 只要数据是以时间为索引，那么我们就可以使用时间进行数据的各种便捷操作了</span></span><br><span class="line"><span class="comment"># 根据时间索引切片筛选出记录</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[pd.Timestamp(<span class="string">'2012-01-01 09:00'</span>):pd.Timestamp(<span class="string">'2012-01-01 19:00'</span>)]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.330750</span>  <span class="number">0.293583</span>  <span class="number">0.029750</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.295000</span>  <span class="number">0.285167</span>  <span class="number">0.031750</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">15</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.301417</span>  <span class="number">0.287750</span>  <span class="number">0.031417</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">18</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.322083</span>  <span class="number">0.304167</span>  <span class="number">0.038083</span></span><br><span class="line"><span class="comment"># df[('2012-01-01 09:00'):('2012-01-01 19:00')] 这种方式等价</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[(<span class="string">'2012-01-01 09:00'</span>):(<span class="string">'2012-01-01 19:00'</span>)]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.330750</span>  <span class="number">0.293583</span>  <span class="number">0.029750</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.295000</span>  <span class="number">0.285167</span>  <span class="number">0.031750</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">15</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.301417</span>  <span class="number">0.287750</span>  <span class="number">0.031417</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">18</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.322083</span>  <span class="number">0.304167</span>  <span class="number">0.038083</span></span><br><span class="line"><span class="comment"># 我们可以直接获取某一年的数据</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[<span class="string">'2013'</span>]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">1.688333</span>  <span class="number">1.688333</span>  <span class="number">0.207333</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">03</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">2.693333</span>  <span class="number">2.693333</span>  <span class="number">0.201500</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">06</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">2.220833</span>  <span class="number">2.220833</span>  <span class="number">0.166917</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">2.055000</span>  <span class="number">2.055000</span>  <span class="number">0.175667</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">1.710000</span>  <span class="number">1.710000</span>  <span class="number">0.129583</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">15</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">1.420000</span>  <span class="number">1.420000</span>  <span class="number">0.096333</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">18</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">1.178583</span>  <span class="number">1.178583</span>  <span class="number">0.083083</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">21</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.898250</span>  <span class="number">0.898250</span>  <span class="number">0.077167</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.860000</span>  <span class="number">0.860000</span>  <span class="number">0.075000</span></span><br><span class="line"><span class="comment"># 也可以指定区间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[<span class="string">'2012-01'</span>:<span class="string">'2012-03'</span>]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.307167</span>  <span class="number">0.273917</span>  <span class="number">0.028000</span></span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span> <span class="number">03</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.302917</span>  <span class="number">0.270833</span>  <span class="number">0.030583</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 由于index为时间格式，因此可以直接使用month等属性</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[df.index.month==<span class="number">1</span>]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.137417</span>  <span class="number">0.097500</span>  <span class="number">0.016833</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">03</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.131250</span>  <span class="number">0.088833</span>  <span class="number">0.016417</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">06</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.113500</span>  <span class="number">0.091250</span>  <span class="number">0.016750</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 复杂一点的过滤</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[(df.index.hour &gt; <span class="number">8</span>) &amp; (df.index.hour &lt; <span class="number">12</span>)]</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.135750</span>  <span class="number">0.091500</span>  <span class="number">0.016250</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-02</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.141917</span>  <span class="number">0.097083</span>  <span class="number">0.016417</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 与上面等价</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.between_time(<span class="string">'08:00'</span>, <span class="string">'12:00'</span>)</span><br><span class="line">                      L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.135750</span>  <span class="number">0.091500</span>  <span class="number">0.016250</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-02</span> <span class="number">09</span>:<span class="number">00</span>:<span class="number">00</span>  <span class="number">0.141917</span>  <span class="number">0.097083</span>  <span class="number">0.016417</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 我们也可以按时间进行重采样</span></span><br><span class="line"><span class="comment"># 比如按天算均值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.resample(<span class="string">'D'</span>).mean()</span><br><span class="line">             L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.125010</span>  <span class="number">0.092281</span>  <span class="number">0.016635</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">0.124146</span>  <span class="number">0.095781</span>  <span class="number">0.016406</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 三天重采样一次</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.resample(<span class="string">'3D'</span>).mean()</span><br><span class="line">             L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.120906</span>  <span class="number">0.091201</span>  <span class="number">0.016378</span></span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.121594</span>  <span class="number">0.091708</span>  <span class="number">0.016670</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 按月重采样一次</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.resample(<span class="string">'M'</span>).mean()</span><br><span class="line">             L06_347  LS06_347  LS06_348</span><br><span class="line">Time</span><br><span class="line"><span class="number">2009</span><span class="number">-01</span><span class="number">-31</span>  <span class="number">0.517864</span>  <span class="number">0.536660</span>  <span class="number">0.045597</span></span><br><span class="line"><span class="number">2009</span><span class="number">-02</span><span class="number">-28</span>  <span class="number">0.516847</span>  <span class="number">0.529987</span>  <span class="number">0.047238</span></span><br><span class="line"><span class="meta">... </span>... ... ...</span><br><span class="line"><span class="comment"># 也可以直接画图，如果使用jupyter notebook，可以使用如下指令</span></span><br><span class="line">%matplotlib notebook</span><br><span class="line">df.resample(<span class="string">'M'</span>).mean().plot()</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="字符串处理"><a href="#字符串处理" class="headerlink" title="字符串处理"></a>字符串处理</h3><p>这些处理方法都只能作用于Series于Index结构</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'A'</span>, <span class="string">'b'</span>, <span class="string">'B'</span>, <span class="string">'gaer'</span>, <span class="string">'AGER'</span>, np.nan])</span><br><span class="line"><span class="comment"># 字符串的大小写转换</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.lower()</span><br><span class="line"><span class="number">0</span>       a</span><br><span class="line"><span class="number">1</span>       b</span><br><span class="line"><span class="number">2</span>       b</span><br><span class="line"><span class="number">3</span>    gaer</span><br><span class="line"><span class="number">4</span>    ager</span><br><span class="line"><span class="number">5</span>     NaN</span><br><span class="line">dtype: object</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.upper()</span><br><span class="line"><span class="number">0</span>       A</span><br><span class="line"><span class="number">1</span>       B</span><br><span class="line"><span class="number">2</span>       B</span><br><span class="line"><span class="number">3</span>    GAER</span><br><span class="line"><span class="number">4</span>    AGER</span><br><span class="line"><span class="number">5</span>     NaN</span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 字符串的长度</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.len()</span><br><span class="line"><span class="number">0</span>    <span class="number">1.0</span></span><br><span class="line"><span class="number">1</span>    <span class="number">1.0</span></span><br><span class="line"><span class="number">2</span>    <span class="number">1.0</span></span><br><span class="line"><span class="number">3</span>    <span class="number">4.0</span></span><br><span class="line"><span class="number">4</span>    <span class="number">4.0</span></span><br><span class="line"><span class="number">5</span>    NaN</span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># 去空格</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = pd.Index([<span class="string">'   jack'</span>, <span class="string">'   straw   '</span>, <span class="string">'xian'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index.str.strip()</span><br><span class="line">Index([<span class="string">'jack'</span>, <span class="string">'straw'</span>, <span class="string">'xian'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index.str.lstrip()</span><br><span class="line">Index([<span class="string">'jack'</span>, <span class="string">'straw   '</span>, <span class="string">'xian'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index.str.rstrip()</span><br><span class="line">Index([<span class="string">'   jack'</span>, <span class="string">'   straw'</span>, <span class="string">'xian'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"><span class="comment"># 字符串替换</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.randn(<span class="number">3</span>,<span class="number">2</span>), columns=[<span class="string">'A a'</span>, <span class="string">'B b'</span>], index=range(<span class="number">3</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">        A a       B b</span><br><span class="line"><span class="number">0</span> <span class="number">-0.863814</span> <span class="number">-0.595908</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0.512255</span>  <span class="number">0.447264</span></span><br><span class="line"><span class="number">2</span>  <span class="number">1.130682</span>  <span class="number">1.472386</span></span><br><span class="line"><span class="comment"># 修改列名</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.columns = df.columns.str.replace(<span class="string">' '</span>, <span class="string">'_'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">        A_a       B_b</span><br><span class="line"><span class="number">0</span> <span class="number">-0.863814</span> <span class="number">-0.595908</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0.512255</span>  <span class="number">0.447264</span></span><br><span class="line"><span class="number">2</span>  <span class="number">1.130682</span>  <span class="number">1.472386</span></span><br><span class="line"><span class="comment"># 字符串切分</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'a_b_C'</span>, <span class="string">'c_d_e'</span>, <span class="string">'f_g_h'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.split(<span class="string">'_'</span>)</span><br><span class="line"><span class="number">0</span>    [a, b, C]</span><br><span class="line"><span class="number">1</span>    [c, d, e]</span><br><span class="line"><span class="number">2</span>    [f, g, h]</span><br><span class="line">dtype: object</span><br><span class="line"><span class="comment"># 如果允许expand，则会变成一个DataFrame结构</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.split(<span class="string">'_'</span>, expand = <span class="keyword">True</span>)</span><br><span class="line">   <span class="number">0</span>  <span class="number">1</span>  <span class="number">2</span></span><br><span class="line"><span class="number">0</span>  a  b  C</span><br><span class="line"><span class="number">1</span>  c  d  e</span><br><span class="line"><span class="number">2</span>  f  g  h</span><br><span class="line"><span class="comment"># 也可以限制切分次数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.split(<span class="string">'_'</span>, expand=<span class="keyword">True</span>, n=<span class="number">1</span>)</span><br><span class="line">   <span class="number">0</span>    <span class="number">1</span></span><br><span class="line"><span class="number">0</span>  a  b_C</span><br><span class="line"><span class="number">1</span>  c  d_e</span><br><span class="line"><span class="number">2</span>  f  g_h</span><br><span class="line"><span class="comment"># 可以查看值是否包含某个字符串序列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'abcde'</span>, <span class="string">'gggbcdiii'</span>, <span class="string">'abiuyf'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.contains(<span class="string">'bcd'</span>)</span><br><span class="line"><span class="number">0</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">1</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">2</span>    <span class="keyword">False</span></span><br><span class="line">dtype: bool</span><br><span class="line"><span class="comment"># 最终是一个DataFrame</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series([<span class="string">'a'</span>, <span class="string">'a|b'</span>, <span class="string">'a|c'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.str.get_dummies(sep=<span class="string">'|'</span>)</span><br><span class="line">   a  b  c</span><br><span class="line"><span class="number">0</span>  <span class="number">1</span>  <span class="number">0</span>  <span class="number">0</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1</span>  <span class="number">1</span>  <span class="number">0</span></span><br><span class="line"><span class="number">2</span>  <span class="number">1</span>  <span class="number">0</span>  <span class="number">1</span></span><br></pre></td></tr></table></figure>
<h2 id="高级主题"><a href="#高级主题" class="headerlink" title="高级主题"></a>高级主题</h2><h3 id="数据透视表"><a href="#数据透视表" class="headerlink" title="数据透视表"></a>数据透视表</h3><p>你可知道数据透视表的作用是什么？搞清楚下面的例子，你便能够了解</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 测试数据如下</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example = pd.DataFrame(&#123;<span class="string">'Month'</span>: [<span class="string">"January"</span>, <span class="string">"January"</span>, <span class="string">"January"</span>, <span class="string">"January"</span>, <span class="string">"February"</span>, <span class="string">"February"</span>, <span class="string">"February"</span>, <span class="string">"February"</span>, <span class="string">"March"</span>, <span class="string">"March"</span>, <span class="string">"March"</span>, <span class="string">"March"</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'Category'</span>: [<span class="string">"Transportation"</span>, <span class="string">"Grocery"</span>, <span class="string">"Household"</span>, <span class="string">"Entertainment"</span>, <span class="string">"Transportation"</span>, <span class="string">"Grocery"</span>, <span class="string">"Household"</span>, <span class="string">"Entertainment"</span>, <span class="string">"Transportation"</span>, <span class="string">"Grocery"</span>, <span class="string">"Household"</span>, <span class="string">"Entertainme</span></span><br><span class="line"><span class="string">nt"</span>],</span><br><span class="line"><span class="meta">... </span>    <span class="string">'Amount'</span>: [<span class="number">74.</span>, <span class="number">235.</span>, <span class="number">175.</span>, <span class="number">100.</span>, <span class="number">115.</span>, <span class="number">240.</span>, <span class="number">225.</span>, <span class="number">125.</span>, <span class="number">90.</span>, <span class="number">260.</span>, <span class="number">200.</span>, <span class="number">120.</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example</span><br><span class="line">       Month        Category  Amount</span><br><span class="line"><span class="number">0</span>    January  Transportation    <span class="number">74.0</span></span><br><span class="line"><span class="number">1</span>    January         Grocery   <span class="number">235.0</span></span><br><span class="line"><span class="number">2</span>    January       Household   <span class="number">175.0</span></span><br><span class="line"><span class="number">3</span>    January   Entertainment   <span class="number">100.0</span></span><br><span class="line"><span class="number">4</span>   February  Transportation   <span class="number">115.0</span></span><br><span class="line"><span class="number">5</span>   February         Grocery   <span class="number">240.0</span></span><br><span class="line"><span class="number">6</span>   February       Household   <span class="number">225.0</span></span><br><span class="line"><span class="number">7</span>   February   Entertainment   <span class="number">125.0</span></span><br><span class="line"><span class="number">8</span>      March  Transportation    <span class="number">90.0</span></span><br><span class="line"><span class="number">9</span>      March         Grocery   <span class="number">260.0</span></span><br><span class="line"><span class="number">10</span>     March       Household   <span class="number">200.0</span></span><br><span class="line"><span class="number">11</span>     March   Entertainment   <span class="number">120.0</span></span><br><span class="line"><span class="comment"># 将散乱的无结构数据转成有结构的数据，换了一种视角，是否更加清楚呢</span></span><br><span class="line"><span class="comment"># 仔细理解一下，透视表得作用</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example_pivot = example.pivot(index=<span class="string">'Category'</span>, columns=<span class="string">'Month'</span>, values=<span class="string">'Amount'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example_pivot</span><br><span class="line">Month           February  January  March</span><br><span class="line">Category</span><br><span class="line">Entertainment      <span class="number">125.0</span>    <span class="number">100.0</span>  <span class="number">120.0</span></span><br><span class="line">Grocery            <span class="number">240.0</span>    <span class="number">235.0</span>  <span class="number">260.0</span></span><br><span class="line">Household          <span class="number">225.0</span>    <span class="number">175.0</span>  <span class="number">200.0</span></span><br><span class="line">Transportation     <span class="number">115.0</span>     <span class="number">74.0</span>   <span class="number">90.0</span></span><br><span class="line"><span class="comment"># 使用透视表后，再进行一些统计就方便多了</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example_pivot.sum(axis=<span class="number">0</span>)</span><br><span class="line">Month</span><br><span class="line">February    <span class="number">705.0</span></span><br><span class="line">January     <span class="number">584.0</span></span><br><span class="line">March       <span class="number">670.0</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>example_pivot.sum(axis=<span class="number">1</span>)</span><br><span class="line">Category</span><br><span class="line">Entertainment     <span class="number">345.0</span></span><br><span class="line">Grocery           <span class="number">735.0</span></span><br><span class="line">Household         <span class="number">600.0</span></span><br><span class="line">Transportation    <span class="number">279.0</span></span><br><span class="line">dtype: float64</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">泰坦尼克号数据实战</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">"titanic_train.csv"</span>)</span><br><span class="line"><span class="comment"># 统计不同性别在不同船舱登记的平均价格如何，这里默认进行的操作就是求均值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Sex'</span>, columns=<span class="string">'Pclass'</span>, values=<span class="string">'Fare'</span>)</span><br><span class="line">Pclass           <span class="number">1</span>          <span class="number">2</span>          <span class="number">3</span></span><br><span class="line">Sex</span><br><span class="line">female  <span class="number">106.125798</span>  <span class="number">21.970121</span>  <span class="number">16.118810</span></span><br><span class="line">male     <span class="number">67.226127</span>  <span class="number">19.741782</span>  <span class="number">12.661633</span></span><br><span class="line"><span class="comment"># 这里我们统计最大值，这里表示不同性别在不同船舱等级的一个最大的花费</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Sex'</span>, columns=<span class="string">'Pclass'</span>, values=<span class="string">'Fare'</span>, aggfunc=<span class="string">'max'</span>)</span><br><span class="line">Pclass         <span class="number">1</span>     <span class="number">2</span>      <span class="number">3</span></span><br><span class="line">Sex</span><br><span class="line">female  <span class="number">512.3292</span>  <span class="number">65.0</span>  <span class="number">69.55</span></span><br><span class="line">male    <span class="number">512.3292</span>  <span class="number">73.5</span>  <span class="number">69.55</span></span><br><span class="line"><span class="comment"># 也可以计数，这里表示不同性别在不同等级船舱的人数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Sex'</span>, columns=<span class="string">'Pclass'</span>, values=<span class="string">'Fare'</span>, aggfunc=<span class="string">'count'</span>)</span><br><span class="line">Pclass    <span class="number">1</span>    <span class="number">2</span>    <span class="number">3</span></span><br><span class="line">Sex</span><br><span class="line">female   <span class="number">94</span>   <span class="number">76</span>  <span class="number">144</span></span><br><span class="line">male    <span class="number">122</span>  <span class="number">108</span>  <span class="number">347</span></span><br><span class="line"><span class="comment"># 提到计数，这里有个便捷的函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.crosstab(index=df[<span class="string">'Sex'</span>], columns=df[<span class="string">'Pclass'</span>])</span><br><span class="line">Pclass    <span class="number">1</span>    <span class="number">2</span>    <span class="number">3</span></span><br><span class="line">Sex</span><br><span class="line">female   <span class="number">94</span>   <span class="number">76</span>  <span class="number">144</span></span><br><span class="line">male    <span class="number">122</span>  <span class="number">108</span>  <span class="number">347</span></span><br><span class="line"><span class="comment"># 统计其他指标，比如统计不同船舱的男女获救情况</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Pclass'</span>, columns=<span class="string">'Sex'</span>, values=<span class="string">'Survived'</span>, aggfunc=<span class="string">'mean'</span>)</span><br><span class="line">Sex       female      male</span><br><span class="line">Pclass</span><br><span class="line"><span class="number">1</span>       <span class="number">0.968085</span>  <span class="number">0.368852</span></span><br><span class="line"><span class="number">2</span>       <span class="number">0.921053</span>  <span class="number">0.157407</span></span><br><span class="line"><span class="number">3</span>       <span class="number">0.500000</span>  <span class="number">0.135447</span></span><br><span class="line"><span class="comment"># 再添加条件，统计未成年人中男性与女性的获救情况</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">'Underaged'</span>] = df[<span class="string">'Age'</span>] &lt; <span class="number">18</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.pivot_table(index=<span class="string">'Underaged'</span>, columns=<span class="string">'Sex'</span>, values=<span class="string">'Survived'</span>, aggfunc=<span class="string">'mean'</span>)</span><br><span class="line">Sex          female      male</span><br><span class="line">Underaged</span><br><span class="line"><span class="keyword">False</span>      <span class="number">0.752896</span>  <span class="number">0.165703</span></span><br><span class="line"><span class="keyword">True</span>       <span class="number">0.690909</span>  <span class="number">0.396552</span></span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="排序操作"><a href="#排序操作" class="headerlink" title="排序操作"></a>排序操作</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = pd.DataFrame(&#123;<span class="string">'group'</span>:[<span class="string">'a'</span>,<span class="string">'a'</span>,<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'b'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'c'</span>,<span class="string">'c'</span>], <span class="string">'data'</span>:[<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">12</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">7</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  group  data</span><br><span class="line"><span class="number">0</span>     a     <span class="number">4</span></span><br><span class="line"><span class="number">1</span>     a     <span class="number">3</span></span><br><span class="line"><span class="number">2</span>     a     <span class="number">2</span></span><br><span class="line"><span class="number">3</span>     b     <span class="number">1</span></span><br><span class="line"><span class="number">4</span>     b    <span class="number">12</span></span><br><span class="line"><span class="number">5</span>     b     <span class="number">3</span></span><br><span class="line"><span class="number">6</span>     c     <span class="number">4</span></span><br><span class="line"><span class="number">7</span>     c     <span class="number">5</span></span><br><span class="line"><span class="number">8</span>     c     <span class="number">7</span></span><br><span class="line"><span class="comment"># 在保证group列的值降序的情况下，data的值升序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.sort_values(by=[<span class="string">'group'</span>, <span class="string">'data'</span>], ascending=[<span class="keyword">False</span>, <span class="keyword">True</span>], inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  group  data</span><br><span class="line"><span class="number">6</span>     c     <span class="number">4</span></span><br><span class="line"><span class="number">7</span>     c     <span class="number">5</span></span><br><span class="line"><span class="number">8</span>     c     <span class="number">7</span></span><br><span class="line"><span class="number">3</span>     b     <span class="number">1</span></span><br><span class="line"><span class="number">5</span>     b     <span class="number">3</span></span><br><span class="line"><span class="number">4</span>     b    <span class="number">12</span></span><br><span class="line"><span class="number">2</span>     a     <span class="number">2</span></span><br><span class="line"><span class="number">1</span>     a     <span class="number">3</span></span><br><span class="line"><span class="number">0</span>     a     <span class="number">4</span></span><br></pre></td></tr></table></figure>
<h3 id="去重"><a href="#去重" class="headerlink" title="去重"></a>去重</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = pd.DataFrame(&#123;<span class="string">'k1'</span>:[<span class="string">'one'</span>]*<span class="number">3</span>+[<span class="string">'two'</span>]*<span class="number">4</span>,<span class="string">'k2'</span>:[<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">4</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">    k1  k2</span><br><span class="line"><span class="number">0</span>  one   <span class="number">3</span></span><br><span class="line"><span class="number">1</span>  one   <span class="number">2</span></span><br><span class="line"><span class="number">2</span>  one   <span class="number">1</span></span><br><span class="line"><span class="number">3</span>  two   <span class="number">3</span></span><br><span class="line"><span class="number">4</span>  two   <span class="number">3</span></span><br><span class="line"><span class="number">5</span>  two   <span class="number">4</span></span><br><span class="line"><span class="number">6</span>  two   <span class="number">4</span></span><br><span class="line"><span class="comment"># 上面的数据有重复值，我们需要删除，只有整条记录相同才会删除</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.drop_duplicates()</span><br><span class="line">    k1  k2</span><br><span class="line"><span class="number">0</span>  one   <span class="number">3</span></span><br><span class="line"><span class="number">1</span>  one   <span class="number">2</span></span><br><span class="line"><span class="number">2</span>  one   <span class="number">1</span></span><br><span class="line"><span class="number">3</span>  two   <span class="number">3</span></span><br><span class="line"><span class="number">5</span>  two   <span class="number">4</span></span><br><span class="line"><span class="comment"># 保证某列的数据时唯一不重复，需要指定去重的列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.drop_duplicates(subset=<span class="string">'k1'</span>)</span><br><span class="line">    k1  k2</span><br><span class="line"><span class="number">0</span>  one   <span class="number">3</span></span><br><span class="line"><span class="number">3</span>  two   <span class="number">3</span></span><br></pre></td></tr></table></figure>
<h3 id="自定义处理方法"><a href="#自定义处理方法" class="headerlink" title="自定义处理方法"></a>自定义处理方法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = pd.DataFrame(&#123;<span class="string">'food'</span>:[<span class="string">'A1'</span>,<span class="string">'A2'</span>,<span class="string">'B1'</span>,<span class="string">'B2'</span>,<span class="string">'B3'</span>,<span class="string">'C1'</span>,<span class="string">'C2'</span>], <span class="string">'data'</span>:[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  food  data</span><br><span class="line"><span class="number">0</span>   A1     <span class="number">1</span></span><br><span class="line"><span class="number">1</span>   A2     <span class="number">2</span></span><br><span class="line"><span class="number">2</span>   B1     <span class="number">3</span></span><br><span class="line"><span class="number">3</span>   B2     <span class="number">4</span></span><br><span class="line"><span class="number">4</span>   B3     <span class="number">5</span></span><br><span class="line"><span class="number">5</span>   C1     <span class="number">6</span></span><br><span class="line"><span class="number">6</span>   C2     <span class="number">7</span></span><br><span class="line"><span class="comment"># 我想要将A1、A2都归为A，B1、B2都归为B，C1、C2都归为C</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">food_map</span><span class="params">(series)</span>:</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">if</span> series[<span class="string">'food'</span>] == <span class="string">'A1'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'A'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'A2'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'A'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'B1'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'B'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'B2'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'B'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'B3'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'B'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'C1'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'C'</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">elif</span> series[<span class="string">'food'</span>] == <span class="string">'C2'</span>:</span><br><span class="line"><span class="meta">... </span>        <span class="keyword">return</span> <span class="string">'C'</span></span><br><span class="line"><span class="comment"># 使用apply函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data[<span class="string">'food_map'</span>] = data.apply(food_map, axis=<span class="string">'columns'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  food  data food_map</span><br><span class="line"><span class="number">0</span>   A1     <span class="number">1</span>        A</span><br><span class="line"><span class="number">1</span>   A2     <span class="number">2</span>        A</span><br><span class="line"><span class="number">2</span>   B1     <span class="number">3</span>        B</span><br><span class="line"><span class="number">3</span>   B2     <span class="number">4</span>        B</span><br><span class="line"><span class="number">4</span>   B3     <span class="number">5</span>        B</span><br><span class="line"><span class="number">5</span>   C1     <span class="number">6</span>        C</span><br><span class="line"><span class="number">6</span>   C2     <span class="number">7</span>        C</span><br><span class="line"><span class="comment"># 使用map函数也能完成目标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>food2Upper = &#123;</span><br><span class="line"><span class="meta">... </span>    <span class="string">'A1'</span>:<span class="string">'A'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'A2'</span>:<span class="string">'A'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B1'</span>:<span class="string">'B'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B2'</span>:<span class="string">'B'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'B3'</span>:<span class="string">'B'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C1'</span>:<span class="string">'C'</span>,</span><br><span class="line"><span class="meta">... </span>    <span class="string">'C2'</span>:<span class="string">'C'</span></span><br><span class="line"><span class="meta">... </span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data[<span class="string">'upper'</span>] = data[<span class="string">'food'</span>].map(food2Upper)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data</span><br><span class="line">  food  data food_map upper</span><br><span class="line"><span class="number">0</span>   A1     <span class="number">1</span>        A     A</span><br><span class="line"><span class="number">1</span>   A2     <span class="number">2</span>        A     A</span><br><span class="line"><span class="number">2</span>   B1     <span class="number">3</span>        B     B</span><br><span class="line"><span class="number">3</span>   B2     <span class="number">4</span>        B     B</span><br><span class="line"><span class="number">4</span>   B3     <span class="number">5</span>        B     B</span><br><span class="line"><span class="number">5</span>   C1     <span class="number">6</span>        C     C</span><br><span class="line"><span class="number">6</span>   C2     <span class="number">7</span>        C     C</span><br><span class="line"><span class="comment"># 请仔细去理解</span></span><br></pre></td></tr></table></figure>
<h3 id="使用assign新增一列，可以使用其他列进行计算"><a href="#使用assign新增一列，可以使用其他列进行计算" class="headerlink" title="使用assign新增一列，可以使用其他列进行计算"></a>使用assign新增一列，可以使用其他列进行计算</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(&#123;<span class="string">'data1'</span>: np.random.randn(<span class="number">5</span>), <span class="string">'data2'</span>: np.random.randn(<span class="number">5</span>)&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">      data1     data2</span><br><span class="line"><span class="number">0</span>  <span class="number">2.453417</span> <span class="number">-0.108647</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1.131228</span>  <span class="number">0.056595</span></span><br><span class="line"><span class="number">2</span> <span class="number">-0.406572</span> <span class="number">-0.675934</span></span><br><span class="line"><span class="number">3</span> <span class="number">-0.534769</span>  <span class="number">0.608112</span></span><br><span class="line"><span class="number">4</span> <span class="number">-0.065837</span> <span class="number">-1.373105</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.assign(ration=df[<span class="string">'data1'</span>]/df[<span class="string">'data2'</span>])</span><br><span class="line">      data1     data2     ration</span><br><span class="line"><span class="number">0</span>  <span class="number">2.453417</span> <span class="number">-0.108647</span> <span class="number">-22.581632</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1.131228</span>  <span class="number">0.056595</span>  <span class="number">19.988231</span></span><br><span class="line"><span class="number">2</span> <span class="number">-0.406572</span> <span class="number">-0.675934</span>   <span class="number">0.601497</span></span><br><span class="line"><span class="number">3</span> <span class="number">-0.534769</span>  <span class="number">0.608112</span>  <span class="number">-0.879392</span></span><br><span class="line"><span class="number">4</span> <span class="number">-0.065837</span> <span class="number">-1.373105</span>   <span class="number">0.047947</span></span><br></pre></td></tr></table></figure>
<h3 id="其他处理"><a href="#其他处理" class="headerlink" title="其他处理"></a>其他处理</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 连续数据的离散化，将多个年龄放到不同区间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ages = [<span class="number">15</span>,<span class="number">18</span>,<span class="number">20</span>,<span class="number">21</span>,<span class="number">22</span>,<span class="number">34</span>,<span class="number">41</span>,<span class="number">52</span>,<span class="number">63</span>,<span class="number">79</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bins = [<span class="number">10</span>,<span class="number">40</span>,<span class="number">80</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bins_res = pd.cut(ages, bins)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>bins_res.codes</span><br><span class="line">array([<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>], dtype=int8)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.value_counts(bins_res)</span><br><span class="line">(<span class="number">10</span>, <span class="number">40</span>]    <span class="number">6</span></span><br><span class="line">(<span class="number">40</span>, <span class="number">80</span>]    <span class="number">4</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 上面都没有指定名字，我们可以指定组名</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ages = [<span class="number">15</span>,<span class="number">18</span>,<span class="number">20</span>,<span class="number">21</span>,<span class="number">22</span>,<span class="number">34</span>,<span class="number">41</span>,<span class="number">52</span>,<span class="number">63</span>,<span class="number">79</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>group_names = [<span class="string">'Yonth'</span>, <span class="string">'Mille'</span>, <span class="string">'Old'</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.value_counts(pd.cut(ages, [<span class="number">10</span>,<span class="number">20</span>,<span class="number">50</span>,<span class="number">80</span>], labels=group_names))</span><br><span class="line">Mille    <span class="number">4</span></span><br><span class="line">Old      <span class="number">3</span></span><br><span class="line">Yonth    <span class="number">3</span></span><br><span class="line">dtype: int64</span><br><span class="line"></span><br><span class="line"><span class="comment"># 处理缺失值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame([range(<span class="number">3</span>),[<span class="number">0</span>, np.nan,<span class="number">0</span>],[<span class="number">0</span>,<span class="number">0</span>,np.nan],range(<span class="number">3</span>)])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">   <span class="number">0</span>    <span class="number">1</span>    <span class="number">2</span></span><br><span class="line"><span class="number">0</span>  <span class="number">0</span>  <span class="number">1.0</span>  <span class="number">2.0</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0</span>  NaN  <span class="number">0.0</span></span><br><span class="line"><span class="number">2</span>  <span class="number">0</span>  <span class="number">0.0</span>  NaN</span><br><span class="line"><span class="number">3</span>  <span class="number">0</span>  <span class="number">1.0</span>  <span class="number">2.0</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.isnull()</span><br><span class="line">       <span class="number">0</span>      <span class="number">1</span>      <span class="number">2</span></span><br><span class="line"><span class="number">0</span>  <span class="keyword">False</span>  <span class="keyword">False</span>  <span class="keyword">False</span></span><br><span class="line"><span class="number">1</span>  <span class="keyword">False</span>   <span class="keyword">True</span>  <span class="keyword">False</span></span><br><span class="line"><span class="number">2</span>  <span class="keyword">False</span>  <span class="keyword">False</span>   <span class="keyword">True</span></span><br><span class="line"><span class="number">3</span>  <span class="keyword">False</span>  <span class="keyword">False</span>  <span class="keyword">False</span></span><br><span class="line"><span class="comment"># 检查每个记录是否有缺失值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.isnull().any(axis=<span class="number">1</span>)</span><br><span class="line"><span class="number">0</span>    <span class="keyword">False</span></span><br><span class="line"><span class="number">1</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">2</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">3</span>    <span class="keyword">False</span></span><br><span class="line">dtype: bool</span><br><span class="line"><span class="comment"># 同样，看每个列是否有缺失值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.isnull().any(axis=<span class="number">0</span>)     <span class="comment"># 默认的axis就是0</span></span><br><span class="line"><span class="number">0</span>    <span class="keyword">False</span></span><br><span class="line"><span class="number">1</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">2</span>     <span class="keyword">True</span></span><br><span class="line">dtype: bool</span><br><span class="line"><span class="comment"># 对于有缺失值的地方，使用用一个数据去填充</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.fillna(<span class="number">5</span>)</span><br><span class="line">   <span class="number">0</span>    <span class="number">1</span>    <span class="number">2</span></span><br><span class="line"><span class="number">0</span>  <span class="number">0</span>  <span class="number">1.0</span>  <span class="number">2.0</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0</span>  <span class="number">5.0</span>  <span class="number">0.0</span></span><br><span class="line"><span class="number">2</span>  <span class="number">0</span>  <span class="number">0.0</span>  <span class="number">5.0</span></span><br><span class="line"><span class="number">3</span>  <span class="number">0</span>  <span class="number">1.0</span>  <span class="number">2.0</span></span><br><span class="line"><span class="comment"># 过滤出有缺失值的行</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[df.isnull().any(axis=<span class="number">1</span>)]</span><br><span class="line">   <span class="number">0</span>    <span class="number">1</span>    <span class="number">2</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0</span>  NaN  <span class="number">0.0</span></span><br><span class="line"><span class="number">2</span>  <span class="number">0</span>  <span class="number">0.0</span>  NaN</span><br><span class="line"><span class="comment"># sklearn库有一个叫做 Imputer 库，专门用于处理缺失值问题</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">'titanic_train.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>obj_df = df.select_dtypes(include=[<span class="string">'object'</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.drop(obj_df.columns, axis=<span class="number">1</span>, inplace=<span class="keyword">True</span>)</span><br><span class="line"><span class="comment"># 我们必须删掉object类型的数据，Imputer才能处理，否则会报错</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> sklearn.preprocessing <span class="keyword">import</span> Imputer</span><br><span class="line"><span class="comment"># 拿到一个带有缺失值的DataFrame</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>impute = pd.DataFrame(Imputer().fit_transform(df))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>impute.columns = df.columns</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>impute.index = df.index</span><br></pre></td></tr></table></figure>
<h3 id="索引高级内容"><a href="#索引高级内容" class="headerlink" title="索引高级内容"></a>索引高级内容</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(np.arange(<span class="number">5</span>), index=np.arange(<span class="number">5</span>)[::<span class="number">-1</span>], dtype=<span class="string">'int64'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line"><span class="number">4</span>    <span class="number">0</span></span><br><span class="line"><span class="number">3</span>    <span class="number">1</span></span><br><span class="line"><span class="number">2</span>    <span class="number">2</span></span><br><span class="line"><span class="number">1</span>    <span class="number">3</span></span><br><span class="line"><span class="number">0</span>    <span class="number">4</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="comment"># 返回s集合中是否都在这个列表中</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.isin([<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>])</span><br><span class="line"><span class="number">4</span>    <span class="keyword">False</span></span><br><span class="line"><span class="number">3</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">2</span>    <span class="keyword">False</span></span><br><span class="line"><span class="number">1</span>     <span class="keyword">True</span></span><br><span class="line"><span class="number">0</span>     <span class="keyword">True</span></span><br><span class="line">dtype: bool</span><br><span class="line"><span class="comment"># 取出对应的内容</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s[s.isin([<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>])]</span><br><span class="line"><span class="number">3</span>    <span class="number">1</span></span><br><span class="line"><span class="number">1</span>    <span class="number">3</span></span><br><span class="line"><span class="number">0</span>    <span class="number">4</span></span><br><span class="line">dtype: int64</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dates = pd.date_range(<span class="string">'20171124'</span>, periods=<span class="number">8</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.randn(<span class="number">8</span>,<span class="number">4</span>), index=dates, columns=[<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'D'</span>])</span><br><span class="line"><span class="comment"># 将大于0的记录都变为8</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.where(df&lt;<span class="number">0</span>, <span class="number">8</span>)</span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-24</span> <span class="number">-0.876734</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span> <span class="number">-2.213080</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-25</span> <span class="number">-1.192806</span>  <span class="number">8.000000</span> <span class="number">-1.032912</span> <span class="number">-0.500371</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-26</span> <span class="number">-0.425647</span> <span class="number">-0.347671</span> <span class="number">-0.976020</span> <span class="number">-0.150681</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-27</span> <span class="number">-1.279070</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-28</span> <span class="number">-0.362680</span> <span class="number">-0.095654</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-29</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span> <span class="number">-0.810233</span> <span class="number">-0.044836</span></span><br><span class="line"><span class="number">2017</span><span class="number">-11</span><span class="number">-30</span>  <span class="number">8.000000</span> <span class="number">-0.036969</span>  <span class="number">8.000000</span>  <span class="number">8.000000</span></span><br><span class="line"><span class="number">2017</span><span class="number">-12</span><span class="number">-01</span> <span class="number">-0.587358</span>  <span class="number">8.000000</span> <span class="number">-1.363178</span>  <span class="number">8.000000</span></span><br><span class="line"><span class="comment"># query，组合复杂的条件</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.rand(<span class="number">10</span>,<span class="number">3</span>), columns=list(<span class="string">'abc'</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">          a         b         c</span><br><span class="line"><span class="number">0</span>  <span class="number">0.925144</span>  <span class="number">0.939164</span>  <span class="number">0.467199</span></span><br><span class="line"><span class="number">1</span>  <span class="number">0.032413</span>  <span class="number">0.865354</span>  <span class="number">0.318904</span></span><br><span class="line"><span class="number">2</span>  <span class="number">0.265597</span>  <span class="number">0.771220</span>  <span class="number">0.318450</span></span><br><span class="line"><span class="number">3</span>  <span class="number">0.643624</span>  <span class="number">0.630970</span>  <span class="number">0.739700</span></span><br><span class="line"><span class="number">4</span>  <span class="number">0.099581</span>  <span class="number">0.409716</span>  <span class="number">0.314810</span></span><br><span class="line"><span class="number">5</span>  <span class="number">0.224205</span>  <span class="number">0.340918</span>  <span class="number">0.380008</span></span><br><span class="line"><span class="number">6</span>  <span class="number">0.309103</span>  <span class="number">0.328867</span>  <span class="number">0.569452</span></span><br><span class="line"><span class="number">7</span>  <span class="number">0.761342</span>  <span class="number">0.545703</span>  <span class="number">0.758707</span></span><br><span class="line"><span class="number">8</span>  <span class="number">0.341552</span>  <span class="number">0.561309</span>  <span class="number">0.989554</span></span><br><span class="line"><span class="number">9</span>  <span class="number">0.797382</span>  <span class="number">0.973130</span>  <span class="number">0.129032</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.query(<span class="string">'a&lt;b &amp; b&lt;c'</span>)</span><br><span class="line">          a         b         c</span><br><span class="line"><span class="number">5</span>  <span class="number">0.224205</span>  <span class="number">0.340918</span>  <span class="number">0.380008</span></span><br><span class="line"><span class="number">6</span>  <span class="number">0.309103</span>  <span class="number">0.328867</span>  <span class="number">0.569452</span></span><br><span class="line"><span class="number">8</span>  <span class="number">0.341552</span>  <span class="number">0.561309</span>  <span class="number">0.989554</span></span><br></pre></td></tr></table></figure>
<h3 id="pandas绘图"><a href="#pandas绘图" class="headerlink" title="pandas绘图"></a>pandas绘图</h3><p>pandas可以直接绘制图形，这里不贴图形内容，只写代码，掌握方法即可<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Series结构画图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = pd.Series(np.random.randn(<span class="number">10</span>), index=np.arange(<span class="number">0</span>,<span class="number">100</span>,<span class="number">10</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line"><span class="number">0</span>    <span class="number">-0.265223</span></span><br><span class="line"><span class="number">10</span>   <span class="number">-0.193092</span></span><br><span class="line"><span class="number">20</span>   <span class="number">-0.929230</span></span><br><span class="line"><span class="number">30</span>   <span class="number">-0.079986</span></span><br><span class="line"><span class="number">40</span>    <span class="number">1.643548</span></span><br><span class="line"><span class="number">50</span>    <span class="number">0.344221</span></span><br><span class="line"><span class="number">60</span>    <span class="number">0.790363</span></span><br><span class="line"><span class="number">70</span>    <span class="number">2.599083</span></span><br><span class="line"><span class="number">80</span>    <span class="number">0.893276</span></span><br><span class="line"><span class="number">90</span>   <span class="number">-0.227786</span></span><br><span class="line">dtype: float64</span><br><span class="line"><span class="comment"># notebook使用魔法指令 %matplotlib inline</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.plot()</span><br><span class="line"></span><br><span class="line"><span class="comment"># DataFrame绘图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.randn(<span class="number">10</span>, <span class="number">4</span>).cumsum(<span class="number">0</span>), </span><br><span class="line"><span class="meta">... </span>           index = np.arange(<span class="number">0</span>, <span class="number">100</span>, <span class="number">10</span>), </span><br><span class="line"><span class="meta">... </span>           columns = [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'D'</span>])</span><br><span class="line">df.plot()</span><br><span class="line"><span class="comment"># 结合matplotlib</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fit,axes = plt.subplots(<span class="number">2</span>,<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = pd.Series(np.random.rand(<span class="number">16</span>),index=list(<span class="string">'abcdefghijklmnop'</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.plot(ax=axes[<span class="number">0</span>],kind=<span class="string">'bar'</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x1180ee208</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.plot(ax=axes[<span class="number">1</span>],kind=<span class="string">'barh'</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x1a19dd16d8</span>&gt;</span><br><span class="line"><span class="comment"># 画柱状图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.DataFrame(np.random.rand(<span class="number">6</span>,<span class="number">4</span>),</span><br><span class="line"><span class="meta">... </span>                 index = [<span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>, <span class="string">'four'</span>, <span class="string">'five'</span>, <span class="string">'six'</span>],</span><br><span class="line"><span class="meta">... </span>                 columns = pd.Index([<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'D'</span>], name=<span class="string">'Genus'</span>))</span><br><span class="line"><span class="comment"># 使用kind制定类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.plot(kind=<span class="string">'bar'</span>)</span><br><span class="line">&lt;matplotlib.axes._subplots.AxesSubplot object at <span class="number">0x1a19f30278</span>&gt;</span><br><span class="line"><span class="comment"># 直方图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>tips = pd.read_csv(<span class="string">'tips.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>tips.total_bill.plot(kind=<span class="string">'hist'</span>, bins=<span class="number">50</span>)</span><br><span class="line"><span class="comment"># 散点图</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>macro = pd.read_csv(<span class="string">'macrodata.csv'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data = macro[[<span class="string">'quarter'</span>, <span class="string">'realgdp'</span>, <span class="string">'realcons'</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>data.plot.scatter(<span class="string">'quarter'</span>,<span class="string">'realgdp'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>pd.scatter_matrix(data, color=<span class="string">'g'</span>, alpha=<span class="number">0.3</span>)</span><br></pre></td></tr></table></figure></p>
<h3 id="大数据处理技巧"><a href="#大数据处理技巧" class="headerlink" title="大数据处理技巧"></a>大数据处理技巧</h3><p>当我们的样本数据非常大的时候，我们直接将其放到内存可能会非常费内存，这里讨论一些可能的优化措施。如果数据非常大，内存怎么优化都存不下，就应该考虑其他方式了，这里提及的大数据处理技巧并非全能</p>
<ol>
<li>如何处理特大的数据<br>这里实验一个291M的数据</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">g1.shape                        <span class="comment"># 可以看到这个数据集的列非常多，处理起来会非常慢</span></span><br><span class="line">g1.info(memory_usage=<span class="string">'deep'</span>)    <span class="comment"># 看一下数据详细情况</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过info函数我们看到元素类型，dtypes: float64(77), int64(6), object(78)</span></span><br><span class="line"><span class="comment"># 我们计算一下每个类型平均占用的内存大小</span></span><br><span class="line"><span class="keyword">for</span> dtype <span class="keyword">in</span> [<span class="string">'float64'</span>, <span class="string">'int64'</span>, <span class="string">'object'</span>]:</span><br><span class="line">    selected_dtype = g1.select_dtypes(include=[dtype])</span><br><span class="line">    mean_usage_b = selected_dtype.memory_usage(deep=<span class="keyword">True</span>).mean()</span><br><span class="line">    mean_usage_mb = mean_usage_b / <span class="number">1024</span> ** <span class="number">2</span></span><br><span class="line">    print(<span class="string">'mean memory usage: '</span>, dtype, mean_usage_mb)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过上面的程序，您看到了不同类型占用的平均大小了吧，我们现在先来优化整数</span></span><br><span class="line"><span class="comment"># 计算一下不同类型的整数能够表示的最大数</span></span><br><span class="line">int_types = [<span class="string">'uint8'</span>, <span class="string">'int8'</span>, <span class="string">'int16'</span>, <span class="string">'int32'</span>, <span class="string">'int64'</span>]</span><br><span class="line"><span class="keyword">for</span> it <span class="keyword">in</span> int_types:</span><br><span class="line">    <span class="keyword">print</span> (np.iinfo(it))</span><br><span class="line"><span class="comment"># 如果你能确定你的样本的数据在对应类型能够表示的范围，就可以转一个最小化内存的类型，为了便于计算，我们定义一个计算内存占用的函数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mem_usage</span><span class="params">(pandas_obj)</span>:</span></span><br><span class="line">    <span class="keyword">if</span> isinstance(pandas_obj, pd.DataFrame):</span><br><span class="line">        usage_b = pandas_obj.memory_usage(deep=<span class="keyword">True</span>).sum()</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        usage_b = pandas_obj.memory_usage(deep=<span class="keyword">True</span>)</span><br><span class="line">    usage_mb = usage_b / <span class="number">1024</span> ** <span class="number">2</span></span><br><span class="line">    <span class="keyword">return</span> <span class="string">'&#123;:03.2f&#125; MB'</span>.format(usage_mb)</span><br><span class="line"><span class="comment"># 下面我们将 int64（默认读取时的类型）进行向下类型转换，然后分别对比一下转换前后的内存占用</span></span><br><span class="line">g1_int = g1.select_dtypes(include=[<span class="string">'int64'</span>])</span><br><span class="line">coverted_int = g1_int.apply(pd.to_numeric, downcast=<span class="string">'unsigned'</span>) <span class="comment"># 将每个元素类型都进行向下类型转换</span></span><br><span class="line">print(mem_usage(g1_int))        <span class="comment"># 7.87 MB</span></span><br><span class="line">print(mem_usage(coverted_int))  <span class="comment"># 1.48 MB</span></span><br><span class="line"><span class="comment"># 以上的对比的方式会了么？对于float也类似，将float64转为float32，内存会省一半</span></span><br><span class="line">g1_float = g1.select_dtypes(include=[<span class="string">'float64'</span>])</span><br><span class="line">coverted_float = g1_float.apply(pd.to_numeric, downcast=<span class="string">'float'</span>)</span><br><span class="line">print(mem_usage(g1_float))</span><br><span class="line">print(mem_usage(coverted_float))</span><br><span class="line"><span class="comment"># 这里我们将原始数据集转化一下，查看整个数据集的内存占用情况</span></span><br><span class="line">optimized_g1 = g1.copy()</span><br><span class="line">optimized_g1[coverted_int.columns] = coverted_int</span><br><span class="line">optimized_g1[coverted_float.columns] = coverted_float</span><br><span class="line">print(mem_usage(g1))                <span class="comment"># 861.57 MB</span></span><br><span class="line">print(mem_usage(optimized_g1))      <span class="comment"># 804.69 MB</span></span><br><span class="line"><span class="comment"># 看着好像也没剩多少，这是因为占用内存最多的是object，下面看看怎么优化object内存</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 优化object类型，我们先看一下object类型都是些什么数据</span></span><br><span class="line">g1_obj = g1.select_dtypes(include=[<span class="string">'object'</span>]).copy()</span><br><span class="line">g1_obj.describe()       </span><br><span class="line"><span class="comment"># 查看结果关注 day_of_week这个列，其类型是object，通过count指标可以看到有171907个数据，查看unique指标可以看到自由7个值，这种数据就是非常好的优化点</span></span><br><span class="line"><span class="comment"># 对于这种只有少数不同值的参数，我们可以使用category类型去替换，这样所有数据就只会占用这7个不同category的内存</span></span><br><span class="line"><span class="comment"># 转换方式也是非常方便</span></span><br><span class="line">dow = g1_obj.day_of_week</span><br><span class="line">dow_cat = dow.astype(<span class="string">'category'</span>)</span><br><span class="line"><span class="comment"># 通过codes来看一下实际转换后的值</span></span><br><span class="line">dow_cat.cat.codes       <span class="comment"># 查看结果是否发现了什么了呢，之前171907个object类型的占用空间，现在只有7个占用空间，明白了么</span></span><br><span class="line"><span class="comment"># 我们再使用上面定义的查看内存占用情况的函数来直观的看一下到底剩了多少空间</span></span><br><span class="line">print(mem_usage(dow))       <span class="comment"># 9.84 MB</span></span><br><span class="line">print(mem_usage(dow_cat))   <span class="comment"># 0.16 MB</span></span><br><span class="line"><span class="comment"># 我们循环得来处理所有的列，检查重复值的比例，如果小于了0.5，我们就转换为category类型</span></span><br><span class="line">converted_obj = pd.DataFrame()</span><br><span class="line"><span class="keyword">for</span> col <span class="keyword">in</span> g1_obj.columns:</span><br><span class="line">    num_unique_values = len(g1_obj[col].unique())</span><br><span class="line">    num_total_values = len(g1_obj[col])</span><br><span class="line">    <span class="keyword">if</span> num_unique_values / num_total_values &lt; <span class="number">0.5</span>:</span><br><span class="line">        converted_obj.loc[:,col] = g1_obj[col].astype(<span class="string">'category'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        converted_obj.loc[:,col] = g1_obj[col]</span><br><span class="line"><span class="comment"># 查看内存情况</span></span><br><span class="line">print(mem_usage(g1_obj))            <span class="comment"># 752.72 MB</span></span><br><span class="line">print(mem_usage(converted_obj))     <span class="comment"># 1.67 MB</span></span><br><span class="line"><span class="comment"># 现在这个优化效果是不是非常棒呢</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 时间类型的优化</span></span><br><span class="line"><span class="comment"># 如果我们的数据有时间类型，那么将其转换为int32类型会更省空间，现在这个数据集中有一个date列，自己试验一下吧</span></span><br></pre></td></tr></table></figure>
<h3 id="读取网络文件"><a href="#读取网络文件" class="headerlink" title="读取网络文件"></a>读取网络文件</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>url = <span class="string">'https://archive.ics.uci.edu/m1/machine-learning-databases/00383/risk_factors_cervical_cancer.csv'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(url, na_values=<span class="string">"?"</span>)        </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.head()</span><br><span class="line"><span class="meta">... </span>...</span><br></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/09/15/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/09/15/1.html" itemprop="url">数据分析之numpy</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-15T08:05:36+08:00">
                2018-09-15
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/" itemprop="url" rel="index">
                    <span itemprop="name">数据分析</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据分析/基础工具/" itemprop="url" rel="index">
                    <span itemprop="name">基础工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/09/15/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/09/15/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/09/15/1.html" class="leancloud_visitors" data-flag-title="数据分析之numpy">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="NumPy"><a href="#NumPy" class="headerlink" title="NumPy"></a>NumPy</h1><p>NumPy（Numeric Python）是一个用于科学计算的基础包。NumPy包含：</p>
<ul>
<li>处理强大的多维数组对象的能力</li>
<li>广播功能函数</li>
<li>整个c/c++与Fortran代码的工具</li>
<li>线性代数、傅里叶变换、随机数生成的能力</li>
</ul>
<p>本教程只涉及一些常用的操作，详细用法请查看 <a href="http://www.numpy.org/">NumPy官方文档</a></p>
<h2 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h2><p>对于一个python的数组，如果要让每个元素的值都加1，该如何实现呢？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = [<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">13</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> idx, element <span class="keyword">in</span> enumerate(arr):</span><br><span class="line"><span class="meta">... </span>    arr[idx] += <span class="number">1</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">[<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]</span><br><span class="line"><span class="comment"># 使用高级python语法，列表推导，或许会更直观些</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[element + <span class="number">1</span> <span class="keyword">for</span> element <span class="keyword">in</span> arr]</span><br><span class="line">[<span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>]</span><br></pre></td></tr></table></figure>
<p>使用NumPy，我们可以更方便的操作，不需要各种循环以及高级语法，本来就是一件很简单的事情</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">13</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr + <span class="number">1</span></span><br><span class="line">array([<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>])</span><br></pre></td></tr></table></figure>
<p>使用<code>np.array</code>包裹上一个数组，就构造了一个<code>ndarray</code>结构的数组了。使用<code>type</code>函数查看这个<code>arr</code>的类型可以得到结果<code>numpy.ndarray</code></p>
<p>NumPy正是使用了这个类为矩阵运算提供了超强的便捷性</p>
<h3 id="ndarray常用属性"><a href="#ndarray常用属性" class="headerlink" title="ndarray常用属性"></a>ndarray常用属性</h3><p>ndarray结构的一些常用属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">1. ndarray.ndim</span><br><span class="line">    数组的维度数量</span><br><span class="line">2. ndarray.shape</span><br><span class="line">    数组的纬数，将返回一个元组来表示维度信息</span><br><span class="line">3. ndarray.size</span><br><span class="line">    数组包含的元素个数</span><br><span class="line">4. ndarray.dtype</span><br><span class="line">    返回数组每个元素的类型。我们在创建的时候可以指定类型，例如 numpy.int32, numpy.int16, numpy.float64等</span><br><span class="line">5. ndarray.itemsize</span><br><span class="line">    返回数组元素所占用的字节大小，例如 float64占8个字节</span><br><span class="line">6. ndarray.nbytes</span><br><span class="line">    返回数组占用的空间，其值等于 ndarray.size * ndarray.itemsize</span><br><span class="line">7. ndarray.flat</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">ndarray属性例子</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = np.arange(<span class="number">15</span>).reshape(<span class="number">3</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">array([[ <span class="number">0</span>,  <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>,  <span class="number">4</span>],</span><br><span class="line">       [ <span class="number">5</span>,  <span class="number">6</span>,  <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.ndim</span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.shape</span><br><span class="line">(<span class="number">3</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.shape = <span class="number">5</span>, <span class="number">3</span>      <span class="comment"># 我们可以直接修改shape值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">array([[ <span class="number">0</span>,  <span class="number">1</span>,  <span class="number">2</span>],</span><br><span class="line">       [ <span class="number">3</span>,  <span class="number">4</span>,  <span class="number">5</span>],</span><br><span class="line">       [ <span class="number">6</span>,  <span class="number">7</span>,  <span class="number">8</span>],</span><br><span class="line">       [ <span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>],</span><br><span class="line">       [<span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.size</span><br><span class="line"><span class="number">15</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.dtype.name</span><br><span class="line"><span class="string">'int64'</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.itemsize</span><br><span class="line"><span class="number">8</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.nbytes</span><br><span class="line"><span class="number">120</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[item <span class="keyword">for</span> item <span class="keyword">in</span> a.flat]                       <span class="comment"># 将多维数组拉平</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="ndarray常用方法"><a href="#ndarray常用方法" class="headerlink" title="ndarray常用方法"></a>ndarray常用方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1. ndarray.fill</span><br><span class="line">    使用指定值填充数组</span><br><span class="line">2. ndarray.copy</span><br><span class="line">    复制数组</span><br><span class="line">3. ndarray.astype</span><br><span class="line">    转换类型</span><br><span class="line">4. ndarray.flatten</span><br><span class="line">    转换为平坦的数组</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">ndarray方法例子</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">0.9455</span>, <span class="number">0.102345</span>, <span class="number">0.11234</span>, <span class="number">0.1298</span>, <span class="number">0.13234</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.fill(<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([ <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr = arr.copy()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr <span class="keyword">is</span> arr</span><br><span class="line"><span class="keyword">False</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.dtype</span><br><span class="line">dtype(<span class="string">'float64'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr = newarr.astype(np.float32)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.dtype</span><br><span class="line">dtype(<span class="string">'float32'</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.flatten()</span><br><span class="line">array([ <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>])</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="创建ndarray数组"><a href="#创建ndarray数组" class="headerlink" title="创建ndarray数组"></a>创建ndarray数组</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">1. numpy.array</span><br><span class="line">    传入列表或元组，也可以指定类型</span><br><span class="line">2. numpy.arange</span><br><span class="line">    生成指定步长的等差数组</span><br><span class="line">3. numpy.linspace</span><br><span class="line">    生成指定最大最小值和元素个数的等差数组</span><br><span class="line">4. numpy.logspace</span><br><span class="line">    生成指定最大最小值和元素个数的等比数组</span><br><span class="line">5. numpy.meshgrid</span><br><span class="line">    指定特定维度数据，创建网格数组</span><br><span class="line">6. numpy.r_</span><br><span class="line">    生成行向量</span><br><span class="line">7. numpy.c_</span><br><span class="line">    生成列向量</span><br><span class="line">8. numpy.zeros</span><br><span class="line">    生成指定shape的值为0矩阵</span><br><span class="line">9. numpy.ones</span><br><span class="line">    生成指定shape的值为1的矩阵</span><br><span class="line">10. numpy.ones_like</span><br><span class="line">    生成与指定矩阵同shape的矩阵</span><br><span class="line">11. numpy.empty</span><br><span class="line">    生成指定shape的空矩阵，初始数据是内存的脏数据</span><br><span class="line">12. numpy.identity</span><br><span class="line">    生成指定shape的单位矩阵</span><br><span class="line">13. numpy.random</span><br><span class="line">    利用random模块生成一个随机的mask数组（值为bool类型，用于mask）</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">创建ndarray数组</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]).shape</span><br><span class="line">(<span class="number">3</span>,)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.array([[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]]).shape</span><br><span class="line">(<span class="number">1</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.arange(<span class="number">5</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.arange(<span class="number">0</span>, <span class="number">5</span>, <span class="number">1</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.arange(<span class="number">0</span>, <span class="number">5</span>, <span class="number">1</span>, dtype=np.int16)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>], dtype=int16)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.linspace(<span class="number">0</span>, <span class="number">10</span>, <span class="number">3</span>)</span><br><span class="line">array([  <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logspace(<span class="number">0</span>, <span class="number">9</span>, <span class="number">10</span>)                   <span class="comment"># 开始值是1，结束值是10^9，生成10个数</span></span><br><span class="line">array([  <span class="number">1.00000000e+00</span>,   <span class="number">1.00000000e+01</span>,   <span class="number">1.00000000e+02</span>,</span><br><span class="line">         <span class="number">1.00000000e+03</span>,   <span class="number">1.00000000e+04</span>,   <span class="number">1.00000000e+05</span>,</span><br><span class="line">         <span class="number">1.00000000e+06</span>,   <span class="number">1.00000000e+07</span>,   <span class="number">1.00000000e+08</span>,</span><br><span class="line">         <span class="number">1.00000000e+09</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.linspace(<span class="number">-10</span>,<span class="number">10</span>,<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.linspace(<span class="number">-10</span>,<span class="number">10</span>,<span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x, y = np.meshgrid(x, y)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">array([[<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>],</span><br><span class="line">       [<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>],</span><br><span class="line">       [<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>],</span><br><span class="line">       [<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>],</span><br><span class="line">       [<span class="number">-10.</span>,  <span class="number">-5.</span>,   <span class="number">0.</span>,   <span class="number">5.</span>,  <span class="number">10.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y</span><br><span class="line">array([[<span class="number">-10.</span>, <span class="number">-10.</span>, <span class="number">-10.</span>, <span class="number">-10.</span>, <span class="number">-10.</span>],</span><br><span class="line">       [ <span class="number">-5.</span>,  <span class="number">-5.</span>,  <span class="number">-5.</span>,  <span class="number">-5.</span>,  <span class="number">-5.</span>],</span><br><span class="line">       [  <span class="number">0.</span>,   <span class="number">0.</span>,   <span class="number">0.</span>,   <span class="number">0.</span>,   <span class="number">0.</span>],</span><br><span class="line">       [  <span class="number">5.</span>,   <span class="number">5.</span>,   <span class="number">5.</span>,   <span class="number">5.</span>,   <span class="number">5.</span>],</span><br><span class="line">       [ <span class="number">10.</span>,  <span class="number">10.</span>,  <span class="number">10.</span>,  <span class="number">10.</span>,  <span class="number">10.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.c_[<span class="number">0</span>:<span class="number">10</span>:<span class="number">3</span>]</span><br><span class="line">array([[<span class="number">0</span>],</span><br><span class="line">       [<span class="number">3</span>],</span><br><span class="line">       [<span class="number">6</span>],</span><br><span class="line">       [<span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.c_[<span class="number">0</span>:<span class="number">10</span>:<span class="number">3</span>].shape</span><br><span class="line">(<span class="number">4</span>, <span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.r_[<span class="number">0</span>:<span class="number">10</span>:<span class="number">3</span>]</span><br><span class="line">array([<span class="number">0</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.r_[<span class="number">0</span>:<span class="number">10</span>:<span class="number">3</span>].shape</span><br><span class="line">(<span class="number">4</span>,)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.zeros(<span class="number">2</span>)</span><br><span class="line">array([ <span class="number">0.</span>,  <span class="number">0.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.zeros((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line">array([[ <span class="number">0.</span>,  <span class="number">0.</span>,  <span class="number">0.</span>],</span><br><span class="line">       [ <span class="number">0.</span>,  <span class="number">0.</span>,  <span class="number">0.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.zeros(<span class="number">2</span>, dtype=np.int16)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">0</span>], dtype=int16)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.ones((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line">array([[ <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>],</span><br><span class="line">       [ <span class="number">1.</span>,  <span class="number">1.</span>,  <span class="number">1.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.ones((<span class="number">2</span>,<span class="number">3</span>)) * <span class="number">8</span></span><br><span class="line">array([[ <span class="number">8.</span>,  <span class="number">8.</span>,  <span class="number">8.</span>],</span><br><span class="line">       [ <span class="number">8.</span>,  <span class="number">8.</span>,  <span class="number">8.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.zeros((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.ones_like(arr, dtype=np.int16)</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>],</span><br><span class="line">       [<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>]], dtype=int16)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.empty((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line">array([[  <span class="number">0.00000000e+000</span>,   <span class="number">0.00000000e+000</span>,   <span class="number">4.22764845e-307</span>],</span><br><span class="line">       [  <span class="number">3.47666793e-309</span>,   <span class="number">0.00000000e+000</span>,   <span class="number">0.00000000e+000</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.identity(<span class="number">3</span>)</span><br><span class="line">array([[ <span class="number">1.</span>,  <span class="number">0.</span>,  <span class="number">0.</span>],</span><br><span class="line">       [ <span class="number">0.</span>,  <span class="number">1.</span>,  <span class="number">0.</span>],</span><br><span class="line">       [ <span class="number">0.</span>,  <span class="number">0.</span>,  <span class="number">1.</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.rand(<span class="number">5</span>) &gt; <span class="number">0.5</span></span><br><span class="line">array([ <span class="keyword">True</span>, <span class="keyword">False</span>,  <span class="keyword">True</span>,  <span class="keyword">True</span>, <span class="keyword">False</span>], dtype=bool)</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="使用切片和索引"><a href="#使用切片和索引" class="headerlink" title="使用切片和索引"></a>使用切片和索引</h3><ol>
<li><p>对于一维数组，操作与python原生数组差别不大</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[<span class="number">2</span>:<span class="number">4</span>]</span><br><span class="line">array([<span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[<span class="number">-2</span>:]</span><br><span class="line">array([<span class="number">4</span>, <span class="number">5</span>])</span><br></pre></td></tr></table></figure>
</li>
<li><p>对于多维数组，如果使用python操作会很麻烦，各种循环。而对于numpy，非常简单</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([</span><br><span class="line"><span class="meta">... </span>    [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>],</span><br><span class="line"><span class="meta">... </span>    [<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>]</span><br><span class="line"><span class="meta">... </span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[:,<span class="number">1</span>]                            <span class="comment"># 获取第一列</span></span><br><span class="line">array([<span class="number">2</span>, <span class="number">7</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[:,<span class="number">2</span>:<span class="number">4</span>]                          <span class="comment"># 获取第二到四列</span></span><br><span class="line">array([[<span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line">    [<span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mask = np.array([                   <span class="comment"># 创建一个mask数组，就是一个bool类型的数组</span></span><br><span class="line"><span class="meta">... </span>    [<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>],</span><br><span class="line"><span class="meta">... </span>    [<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>]</span><br><span class="line"><span class="meta">... </span>], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[mask]                           <span class="comment"># 如果mask某个位置的值为true，那么就取arr数组对应的元素，否则就不取</span></span><br><span class="line">array([ <span class="number">2</span>,  <span class="number">6</span>,  <span class="number">8</span>, <span class="number">10</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mask = np.random.rand(<span class="number">2</span>, <span class="number">5</span>) &gt; <span class="number">0.5</span>   <span class="comment"># 生成随机mask数组</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[mask]</span><br><span class="line">array([<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>])</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="数组运算"><a href="#数组运算" class="headerlink" title="数组运算"></a>数组运算</h3><h4 id="关于数值的计算"><a href="#关于数值的计算" class="headerlink" title="关于数值的计算"></a>关于数值的计算</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> numpy.sum</span><br><span class="line">    可以求所有元素的和，也可以按轴求和</span><br><span class="line"><span class="number">2.</span> numpy.prod</span><br><span class="line">    计算乘积</span><br><span class="line"><span class="number">3.</span> numpy.min/numpy.max</span><br><span class="line">    找最小/大值，可以拉通了找，也可以按轴找</span><br><span class="line"><span class="number">4.</span> numpy.argmin/numpy.argmax</span><br><span class="line">    找最小/大值所在的索引，可以拉通了找，也可以按轴找</span><br><span class="line"><span class="number">5.</span> numpy.mean</span><br><span class="line">    均值</span><br><span class="line"><span class="number">6.</span> numpy.std</span><br><span class="line">    标准差</span><br><span class="line"><span class="number">7.</span> numpy.var</span><br><span class="line">    方差</span><br><span class="line"><span class="number">8.</span> numpy.clip</span><br><span class="line">    限制，看代码</span><br><span class="line"><span class="number">9.</span> numpy.round</span><br><span class="line">    四舍五入，可以设置精度</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">数值计算实践</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([</span><br><span class="line"><span class="meta">... </span>    [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],</span><br><span class="line"><span class="meta">... </span>    [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line"><span class="meta">... </span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.sum()               <span class="comment"># 等价 np.sum(arr)</span></span><br><span class="line"><span class="number">21</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.sum(axis=<span class="number">0</span>)         <span class="comment"># 等价 np.sum(arr, axis=0) </span></span><br><span class="line">array([<span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.sum(axis=<span class="number">1</span>)         <span class="comment"># 等价 np.sum(arr, axis=1)</span></span><br><span class="line">array([ <span class="number">6</span>, <span class="number">15</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.prod()</span><br><span class="line"><span class="number">720</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.prod(axis=<span class="number">0</span>)</span><br><span class="line">array([ <span class="number">4</span>, <span class="number">10</span>, <span class="number">18</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.prod(axis=<span class="number">1</span>)</span><br><span class="line">array([  <span class="number">6</span>, <span class="number">120</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.min()</span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.min(axis=<span class="number">0</span>)</span><br><span class="line">array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.min(axis=<span class="number">1</span>)</span><br><span class="line">array([<span class="number">1</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.max()</span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.max(axis=<span class="number">0</span>)</span><br><span class="line">array([<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.max(axis=<span class="number">1</span>)</span><br><span class="line">array([<span class="number">3</span>, <span class="number">6</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmax()            <span class="comment"># 这里是flat后的序号</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmax(axis=<span class="number">0</span>)</span><br><span class="line">array([<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmax(axis=<span class="number">1</span>)</span><br><span class="line">array([<span class="number">2</span>, <span class="number">2</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmin()            <span class="comment"># 这里是flat后的序号</span></span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmin(axis=<span class="number">0</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argmin(axis=<span class="number">1</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">0</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.mean()</span><br><span class="line"><span class="number">3.5</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.mean(axis=<span class="number">0</span>)</span><br><span class="line">array([ <span class="number">2.5</span>,  <span class="number">3.5</span>,  <span class="number">4.5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.mean(axis=<span class="number">1</span>)</span><br><span class="line">array([ <span class="number">2.</span>,  <span class="number">5.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.std()</span><br><span class="line"><span class="number">1.707825127659933</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.std(axis=<span class="number">0</span>)</span><br><span class="line">array([ <span class="number">1.5</span>,  <span class="number">1.5</span>,  <span class="number">1.5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.std(axis=<span class="number">1</span>)</span><br><span class="line">array([ <span class="number">0.81649658</span>,  <span class="number">0.81649658</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.var()</span><br><span class="line"><span class="number">2.9166666666666665</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.var(axis=<span class="number">0</span>)</span><br><span class="line">array([ <span class="number">2.25</span>,  <span class="number">2.25</span>,  <span class="number">2.25</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.var(axis=<span class="number">1</span>)</span><br><span class="line">array([ <span class="number">0.66666667</span>,  <span class="number">0.66666667</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.clip(<span class="number">2</span>,<span class="number">4</span>)                   <span class="comment"># 指定最大最小值，大于最大值的取最大值，小于最小值的取最小值</span></span><br><span class="line">array([[<span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">       [<span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.round(decimals=<span class="number">1</span>)</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">       [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]])</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="关于矩阵的计算"><a href="#关于矩阵的计算" class="headerlink" title="关于矩阵的计算"></a>关于矩阵的计算</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1. numpy.multiply</span><br><span class="line">    与运算符号*等价，表示乘法，对应位置分别相乘，一般很少用</span><br><span class="line">2. numpy.logical_&#123;and,or,xor,not&#125;</span><br><span class="line">    一系列的逻辑操作，并、或、异或、非</span><br><span class="line">3. numpy.dot</span><br><span class="line">    矩阵乘法运算，常用</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">矩阵计算实践</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]).reshape(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.array([<span class="number">2</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]).reshape(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">       [<span class="number">3</span>, <span class="number">4</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y</span><br><span class="line">array([[<span class="number">2</span>, <span class="number">4</span>],</span><br><span class="line">       [<span class="number">5</span>, <span class="number">6</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.multiply(x,y)            <span class="comment"># 与 x * y 等价</span></span><br><span class="line">array([[ <span class="number">2</span>,  <span class="number">8</span>],</span><br><span class="line">       [<span class="number">15</span>, <span class="number">24</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x * y</span><br><span class="line">array([[ <span class="number">2</span>,  <span class="number">8</span>],</span><br><span class="line">       [<span class="number">15</span>, <span class="number">24</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]).reshape(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.array([<span class="number">2</span>,<span class="number">0</span>,<span class="number">5</span>,<span class="number">0</span>]).reshape(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_and(x,y)</span><br><span class="line">array([[ <span class="keyword">True</span>, <span class="keyword">False</span>],</span><br><span class="line">       [ <span class="keyword">True</span>, <span class="keyword">False</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_or(x,y)</span><br><span class="line">array([[ <span class="keyword">True</span>,  <span class="keyword">True</span>],</span><br><span class="line">       [ <span class="keyword">True</span>,  <span class="keyword">True</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_xor(x,y)</span><br><span class="line">array([[<span class="keyword">False</span>,  <span class="keyword">True</span>],</span><br><span class="line">       [<span class="keyword">False</span>,  <span class="keyword">True</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_not(x)</span><br><span class="line">array([[<span class="keyword">False</span>, <span class="keyword">False</span>],</span><br><span class="line">       [<span class="keyword">False</span>, <span class="keyword">False</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.logical_not(y)</span><br><span class="line">array([[<span class="keyword">False</span>,  <span class="keyword">True</span>],</span><br><span class="line">       [<span class="keyword">False</span>,  <span class="keyword">True</span>]], dtype=bool)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = np.array([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]).reshape(<span class="number">1</span>,<span class="number">4</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = np.array([<span class="number">2</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]).reshape(<span class="number">4</span>,<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.dot(x,y)                 <span class="comment"># 常用的矩阵相乘</span></span><br><span class="line">array([[<span class="number">49</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.dot(y, x)</span><br><span class="line">array([[ <span class="number">2</span>,  <span class="number">4</span>,  <span class="number">6</span>,  <span class="number">8</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">8</span>, <span class="number">12</span>, <span class="number">16</span>],</span><br><span class="line">       [ <span class="number">5</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">20</span>],</span><br><span class="line">       [ <span class="number">6</span>, <span class="number">12</span>, <span class="number">18</span>, <span class="number">24</span>]])</span><br></pre></td></tr></table></figure>

</div></div>
<h3 id="排序操作"><a href="#排序操作" class="headerlink" title="排序操作"></a>排序操作</h3><p>排序操作是数据分析过程中一个高频操作，这里看看如何使用numpy进行矩阵元素排序</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">1. ndarray.sort</span><br><span class="line">2. ndarray.argsort</span><br><span class="line">    不改变原始数组，返回排序后，新数据在原数据中的位置</span><br><span class="line">3. ndarray.searchsorted</span><br><span class="line">    在一个有序的序列中插入数据，算其插入位置</span><br><span class="line">4. numpy.lexsort</span><br><span class="line">    针对不同的列进行不同的排序</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">数组排序实践</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">5.6</span>, <span class="number">1.3</span>, <span class="number">7.5</span>, <span class="number">1.5</span>, <span class="number">7.8</span>, <span class="number">1.2</span>]).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.sort(axis=<span class="number">0</span>)            <span class="comment"># 按第一维排序，会修改原数组，如果不知道axis将拉平了后进行排序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[ <span class="number">1.5</span>,  <span class="number">1.3</span>,  <span class="number">1.2</span>],</span><br><span class="line">       [ <span class="number">5.6</span>,  <span class="number">7.8</span>,  <span class="number">7.5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">5.6</span>, <span class="number">1.3</span>, <span class="number">7.5</span>, <span class="number">1.5</span>, <span class="number">7.8</span>, <span class="number">1.2</span>]).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[ <span class="number">5.6</span>,  <span class="number">1.3</span>,  <span class="number">7.5</span>],</span><br><span class="line">       [ <span class="number">1.5</span>,  <span class="number">7.8</span>,  <span class="number">1.2</span>]])</span><br><span class="line"><span class="comment"># 该函数将不会修改原数组，会返回排序后的数据在原始数组中的坐标，默认axis参数为1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.argsort()</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">0</span>, <span class="number">2</span>],</span><br><span class="line">       [<span class="number">2</span>, <span class="number">0</span>, <span class="number">1</span>]])</span><br><span class="line"><span class="comment"># 想要将一系列值插入到一个有序的序列，但是不知道应该插在什么位置，searchsorted函数用于获取该位置索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.linspace(<span class="number">0</span>, <span class="number">9</span>, <span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([ <span class="number">0.</span>,  <span class="number">1.</span>,  <span class="number">2.</span>,  <span class="number">3.</span>,  <span class="number">4.</span>,  <span class="number">5.</span>,  <span class="number">6.</span>,  <span class="number">7.</span>,  <span class="number">8.</span>,  <span class="number">9.</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>values = np.array([<span class="number">2.5</span>, <span class="number">6.5</span>, <span class="number">9.5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.searchsorted(arr, values)</span><br><span class="line">array([ <span class="number">3</span>,  <span class="number">7</span>, <span class="number">10</span>])</span><br><span class="line"><span class="comment"># 如果我们想要针对不同列排序，可以使用lexsort</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.array([<span class="number">1</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">0</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">0</span>]).reshape(<span class="number">4</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">0</span>, <span class="number">6</span>],</span><br><span class="line">       [<span class="number">1</span>, <span class="number">7</span>, <span class="number">0</span>],</span><br><span class="line">       [<span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>],</span><br><span class="line">       [<span class="number">2</span>, <span class="number">4</span>, <span class="number">0</span>]])</span><br><span class="line"><span class="comment"># 在保证index为2的列升序的情况下，index为0的列要实现降序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>index = np.lexsort([<span class="number">-1</span> * arr[:,<span class="number">0</span>], arr[:,<span class="number">2</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr[index]</span><br><span class="line">array([[<span class="number">2</span>, <span class="number">4</span>, <span class="number">0</span>],</span><br><span class="line">       [<span class="number">1</span>, <span class="number">7</span>, <span class="number">0</span>],</span><br><span class="line">       [<span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>],</span><br><span class="line">       [<span class="number">1</span>, <span class="number">0</span>, <span class="number">6</span>]])</span><br></pre></td></tr></table></figure>

</div></div>
<h2 id="进阶"><a href="#进阶" class="headerlink" title="进阶"></a>进阶</h2><p>如果看到了这里，恭喜你，您已经非常棒了，看来您对numpy的兴趣还是非常强的，下面我们介绍一些高级点的内容</p>
<h3 id="数组形状的变更"><a href="#数组形状的变更" class="headerlink" title="数组形状的变更"></a>数组形状的变更</h3><ol>
<li><p>改变矩阵的维度</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.arange(<span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.shape = <span class="number">2</span>, <span class="number">5</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line">    [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.shape = <span class="number">5</span>, <span class="number">2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">6</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="comment"># 我们也可以直接使用reshape函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.reshape(<span class="number">2</span>, <span class="number">5</span>)</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line">    [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="comment"># 将矩阵变成一个向量，拉平</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.flatten()</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.ravel()</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>])</span><br></pre></td></tr></table></figure>
</li>
<li><p>增加与删除维度</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.arange(<span class="number">10</span>).reshape(<span class="number">5</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">6</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.shape</span><br><span class="line">(<span class="number">5</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr = arr[np.newaxis, :, :]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr</span><br><span class="line">array([[[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">        [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">        [<span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">        [<span class="number">6</span>, <span class="number">7</span>],</span><br><span class="line">        [<span class="number">8</span>, <span class="number">9</span>]]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.shape</span><br><span class="line">(<span class="number">1</span>, <span class="number">5</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr = arr[:, :, np.newaxis]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr</span><br><span class="line">array([[[<span class="number">0</span>],</span><br><span class="line">        [<span class="number">1</span>]],</span><br><span class="line"></span><br><span class="line">        [[<span class="number">2</span>],</span><br><span class="line">        [<span class="number">3</span>]],</span><br><span class="line"></span><br><span class="line">        [[<span class="number">4</span>],</span><br><span class="line">        [<span class="number">5</span>]],</span><br><span class="line"></span><br><span class="line">        [[<span class="number">6</span>],</span><br><span class="line">        [<span class="number">7</span>]],</span><br><span class="line"></span><br><span class="line">        [[<span class="number">8</span>],</span><br><span class="line">        [<span class="number">9</span>]]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.shape</span><br><span class="line">(<span class="number">5</span>, <span class="number">2</span>, <span class="number">1</span>)</span><br><span class="line"><span class="comment"># 删除多余没用的维度，也称为压缩操作</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>newarr.squeeze()</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">6</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">8</span>, <span class="number">9</span>]])</span><br></pre></td></tr></table></figure>
</li>
<li><p>获取转置矩阵</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 求转置矩阵</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.arange(<span class="number">10</span>).reshape(<span class="number">2</span>, <span class="number">5</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line">    [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.transpose()</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">1</span>, <span class="number">6</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">3</span>, <span class="number">8</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr.T</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">5</span>],</span><br><span class="line">    [<span class="number">1</span>, <span class="number">6</span>],</span><br><span class="line">    [<span class="number">2</span>, <span class="number">7</span>],</span><br><span class="line">    [<span class="number">3</span>, <span class="number">8</span>],</span><br><span class="line">    [<span class="number">4</span>, <span class="number">9</span>]])</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="连接矩阵"><a href="#连接矩阵" class="headerlink" title="连接矩阵"></a>连接矩阵</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = (np.arange(<span class="number">6</span>) + <span class="number">1</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">       [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = (np.arange(<span class="number">6</span>) + <span class="number">7</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b</span><br><span class="line">array([[ <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.concatenate((a, b), axis=<span class="number">1</span>)</span><br><span class="line">array([[ <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>,  <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.concatenate((a, b), axis=<span class="number">0</span>)</span><br><span class="line">array([[ <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>],</span><br><span class="line">       [ <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br><span class="line"><span class="comment"># 如果待链接的矩阵是二维的，那么还可以用函数vstack和hstack</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.hstack((a, b))</span><br><span class="line">array([[ <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>,  <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.vstack((a, b))</span><br><span class="line">array([[ <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>],</span><br><span class="line">       [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>],</span><br><span class="line">       [ <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">       [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]])</span><br></pre></td></tr></table></figure>
<h3 id="随机模块"><a href="#随机模块" class="headerlink" title="随机模块"></a>随机模块</h3><p>随机值在机器学习中非常重要，在进行一个模型训练前都会选择一个随机值进行处理，这里我们一起学习一下吧</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1. numpy.random.rand</span><br><span class="line">    产生0到1的随机数，可以指定要产生矩阵的shape，默认只产生一个数</span><br><span class="line">2. numpy.random.random_sample</span><br><span class="line">    就产生一个随机数，与 numpy.random.rand()等价</span><br><span class="line">3. numpy.random.randint</span><br><span class="line">    用于产生整数，可以制定范围、个数以及shape</span><br></pre></td></tr></table></figure>
<div><div class="fold_hider"><div class="close hider_title">随机模块实践</div></div><div class="fold">
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.rand()</span><br><span class="line"><span class="number">0.9083146428390426</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.rand(<span class="number">3</span>)</span><br><span class="line">array([ <span class="number">0.21180848</span>,  <span class="number">0.72211183</span>,  <span class="number">0.73075582</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.rand(<span class="number">2</span>, <span class="number">2</span>)</span><br><span class="line">array([[ <span class="number">0.66070728</span>,  <span class="number">0.3936634</span> ],</span><br><span class="line">       [ <span class="number">0.07193046</span>,  <span class="number">0.67754269</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.random_sample()</span><br><span class="line"><span class="number">0.6710709363984259</span></span><br><span class="line"><span class="comment"># 如果就一个整数，表示0到该参数内取整数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">10</span>)</span><br><span class="line"><span class="number">7</span></span><br><span class="line"><span class="comment"># 如果两个参数，表示最大最小值内取整数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">-10</span>, <span class="number">10</span>)</span><br><span class="line"><span class="number">-6</span></span><br><span class="line"><span class="comment"># 如果有size参数，则表示产生的矩阵shape</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">-10</span>, <span class="number">10</span>, size=(<span class="number">2</span>,<span class="number">2</span>))</span><br><span class="line">array([[ <span class="number">8</span>, <span class="number">-5</span>],</span><br><span class="line">       [<span class="number">-2</span>, <span class="number">-9</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">10</span>, size=(<span class="number">2</span>,<span class="number">2</span>))</span><br><span class="line">array([[<span class="number">5</span>, <span class="number">9</span>],</span><br><span class="line">       [<span class="number">3</span>, <span class="number">5</span>]])</span><br><span class="line"><span class="comment"># 下面两种等价，产生一个向量</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">-10</span>, <span class="number">10</span>, size=(<span class="number">3</span>,))</span><br><span class="line">array([<span class="number">-3</span>, <span class="number">-6</span>,  <span class="number">9</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.randint(<span class="number">-10</span>, <span class="number">10</span>, <span class="number">3</span>)</span><br><span class="line">array([<span class="number">-2</span>, <span class="number">-3</span>,  <span class="number">2</span>])</span><br><span class="line"><span class="comment"># 我们也可以产生满足高斯分布的向量</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mu, sigma = <span class="number">0</span>, <span class="number">0.1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.normal(mu, sigma, <span class="number">10</span>)</span><br><span class="line">array([<span class="number">-0.10791302</span>,  <span class="number">0.08377314</span>,  <span class="number">0.03925479</span>, <span class="number">-0.06169874</span>,  <span class="number">0.06336937</span>,</span><br><span class="line">        <span class="number">0.08090765</span>, <span class="number">-0.19557721</span>, <span class="number">-0.0209661</span> , <span class="number">-0.02408436</span>,  <span class="number">0.19880881</span>])</span><br><span class="line"><span class="comment"># 我们可以使用shuffle函数打乱向量的顺序，只能是向量</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = np.arange(<span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.shuffle(arr)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr</span><br><span class="line">array([<span class="number">0</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">6</span>, <span class="number">4</span>])</span><br><span class="line"><span class="comment"># 计算机的随机数都是伪随机，有一个种子的概念，只要种子相同，产生的随机数就一定相同</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>np.random.seed(<span class="number">0</span>)</span><br></pre></td></tr></table></figure>

</div></div>
<h2 id="高阶"><a href="#高阶" class="headerlink" title="高阶"></a>高阶</h2><h3 id="文件读写"><a href="#文件读写" class="headerlink" title="文件读写"></a>文件读写</h3><p>文件读写也是我们经常使用的一个操作，从文件中读取文件，然后让numpy去处理</p>
<ol>
<li><p>读取一个文本文件</p>
<p> 可以自己手动写一个txt文件，如果使用anaconda，也可以通过 <code>%%writefile arr.txt</code>写一个文件</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8</span><br><span class="line">2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure>
<p> 这里我们比较一下，使用python读取文件与numpy读取文件的区别，你便可以了解到哪个更方便</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">######### 使用python #########</span></span><br><span class="line">data = []</span><br><span class="line"><span class="keyword">with</span> open(<span class="string">"arr.txt"</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f.readlines():</span><br><span class="line">        fields = line.split()   <span class="comment"># 默认以空格分割</span></span><br><span class="line">        cur_data = [float(x) <span class="keyword">for</span> x <span class="keyword">in</span> fields]</span><br><span class="line">        data.append(cur_data)</span><br><span class="line">data = np.array(data)</span><br><span class="line"><span class="comment">######### 使用使用numpy #########</span></span><br><span class="line">data = np.loadtxt(<span class="string">'arr.txt'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>读取一个文本文件（指定分隔符）</p>
<p> 上面的例子我们的文件是以空格作文分隔符区分每一个元素的，那如果我们的文件是逗号呢？</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 假设文件内容如下</span></span><br><span class="line"><span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span></span><br><span class="line"><span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span></span><br><span class="line"><span class="comment"># 我们需要指定逗号为分隔符</span></span><br><span class="line">data = np.loadtxt(<span class="string">'arr.txt'</span>, delimiter=<span class="string">','</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>读取一个文本文件（带有头信息）</p>
<p> 很多时候其实我们的文件可能不只是有数据内容，可能每列的头信息也标识出来了，像下面这样，如果还是使用上面的读取方式，又报错，那怎么办呢</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 假设文件内容如下</span><br><span class="line">a,b,c,d,e,f,g,h</span><br><span class="line">1,2,3,4,5,6,7,8</span><br><span class="line">2,3,4,5,6,7,8,9</span><br><span class="line"># 对于这种我们可以通过参数跳过某一列不读取</span><br><span class="line">data = np.loadtxt(&apos;arr.txt&apos;, delimiter=&apos;,&apos;, skiprows=1)     # 这里的参数skiprows=1，前面的1行都不读取，如果设置5，表示前面5行都不读取</span><br></pre></td></tr></table></figure>
</li>
<li><p>读取一个文本文件（只读取指定的列）</p>
<p> 很多时候文件记录的内容很多，但是我们需要的可能就某几列而已，那么可以用如下的方式指定列</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 只读取0，2，3列的内容</span></span><br><span class="line">data = np.loadtxt(<span class="string">'arr.txt'</span>, delimiter=<span class="string">','</span>, skiprows=<span class="number">1</span>, usecols=(<span class="number">0</span>,<span class="number">2</span>,<span class="number">3</span>))</span><br></pre></td></tr></table></figure>
</li>
<li><p>上面4个点我们讨论的都是怎么去读取一个文本文件，那我们是否有方式将我们的数组保存到文件呢，当然有啦</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">data = np.array([</span><br><span class="line">    [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line">])</span><br><span class="line">np.savetxt(<span class="string">'myarr.txt'</span>, data, delimiter=<span class="string">','</span>)</span><br><span class="line"><span class="comment"># 去看看文件，我们发现写入的内容都是科学计数法的，比较大，能否简化呢？</span></span><br><span class="line">np.savetxt(<span class="string">'myarr.txt'</span>, data, delimiter=<span class="string">','</span>, fmt=<span class="string">'%d'</span>)  <span class="comment"># 添加fmt参数指定保存的格式，%d 表示整数，也可以用其他的，比如 %.2f 有两位小数的浮点数</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>我们的数据都保存成了txt文件，是可以打开看到内容，其实我们的数据只要程序能看得懂就可以了，我们看不看得无所谓，因此一般也会将这种数据直接保存为二进制格式，节省空间</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">data = np.array([</span><br><span class="line">    [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],</span><br><span class="line">    [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line">])</span><br><span class="line">np.save(<span class="string">'myarr.npy'</span>, data)  <span class="comment"># 执行完了后，我们能在当前目录看到 myarr.npy 的文件，但是打开是看不懂内容的，这就是二进制格式存储的</span></span><br><span class="line">data = np.load(<span class="string">'myarr.npy'</span>) <span class="comment"># 读取也是非常简单，使用load方法即可</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>第6点我们讲到了如何将数组保存到一个二进制文件中，我们的例子只是一个数组，那要是多个数组呢？应该怎么保存于读取呢？</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">x = np.arange(<span class="number">0</span>,<span class="number">10</span>,<span class="number">1</span>)</span><br><span class="line">y = np.arange(<span class="number">10</span>,<span class="number">20</span>,<span class="number">1</span>)</span><br><span class="line">np.savez(<span class="string">'myarr.npz'</span>, x=x, y=y) <span class="comment"># 使用savez函数将多个数组保存到一个npz格式的文件中，x，y都是自己写的，也可以写a，b等等</span></span><br><span class="line">data = np.load(<span class="string">'myarr.npz'</span>)     <span class="comment"># 读取还是使用的load函数，没有什么不同，只是如果读取的文件是多个数组压缩保存的文件，那load函数返回的不是一个数组，而是一个数组集合，使用方式如下</span></span><br><span class="line"></span><br><span class="line">data.keys()                     <span class="comment"># 获取keys</span></span><br><span class="line">data[<span class="string">'x'</span>]                       <span class="comment"># 通过这种方式读取对应的数组内容</span></span><br><span class="line">data[<span class="string">'y'</span>]</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="其他常规选项"><a href="#其他常规选项" class="headerlink" title="其他常规选项"></a>其他常规选项</h2><ol>
<li>设置浮点数据的显示精度</li>
</ol>
<p>注意这里只是显示上设置了，原始数据并没有改变，主要方便我们查看</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">np.set_printoptions(precision=<span class="number">3</span>)        <span class="comment"># 设置显示的精度</span></span><br></pre></td></tr></table></figure>
<h2 id="练习题"><a href="#练习题" class="headerlink" title="练习题"></a>练习题</h2><p>计算机是一门动手的科学，想要学好，必须实践，相信你可以的</p>
<ol>
<li>打印当前Numpy版本</li>
<li>构造一个全零的矩阵，并打印其占用的内存大小</li>
<li>打印一个函数的帮助文档，比如numpy.add</li>
<li>创建一个10-49的数组，并将其倒序排序</li>
<li>找到一个数组中不为0的索引</li>
<li>随机构造一个3*3矩阵，并打印其中最大与最小值</li>
<li>构造一个5*5的矩阵，令其值都为1，并在最外层加上一圈0</li>
<li>构建一个shape为(6,7,8)的矩阵，并找到第100个元素的索引值</li>
<li>对一个5*5的矩阵做归一化操作</li>
<li>找到两个数组中相同的值</li>
<li>得到今天、明天、昨天的日期</li>
<li>得到一个月中所有的天</li>
<li>得到一个数的整数部分</li>
<li>构造一个数组，让它不能被改变</li>
<li>打印大数据的部分值，全部值</li>
<li>找到在一个数组中，最接近一个数的索引</li>
<li>32位float类型和32位int类型转换</li>
<li>打印数组元素位置坐标与数值</li>
<li>按照数组的某一列进行排序</li>
<li>统计数组中每个数值出现的次数</li>
<li>如何对一个四维数组的最后两维来求和</li>
<li>交换矩阵中的两行</li>
<li>找到一个数组中最常出现的数字</li>
<li>快速查找TOP K</li>
<li>去除掉一个数组中，所有元素都相同的数据</li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/09/04/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/09/04/1.html" itemprop="url">k8s dashboard 源码分析（一）</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-04T11:11:19+08:00">
                2018-09-04
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/09/04/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/09/04/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/09/04/1.html" class="leancloud_visitors" data-flag-title="k8s dashboard 源码分析（一）">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h1><p>dashboard提供了访问k8s服务的界面</p>
<h2 id="源码分析"><a href="#源码分析" class="headerlink" title="源码分析"></a>源码分析</h2><p>dashboard分多个模块，我们逐一分析</p>
<h3 id="clientManager"><a href="#clientManager" class="headerlink" title="clientManager"></a>clientManager</h3><p>clientManager模块用于访问k8s的rest 接口服务</p>
<figure class="highlight plain"><figcaption><span>/dashboard.go[+94]</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">clientManager := client.NewClientManager(args.Holder.GetKubeConfigFile(), args.Holder.GetApiServerHost())</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><figcaption><span>/client/manager.go[+359]</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">func NewClientManager(kubeConfigPath, apiserverHost string) clientapi.ClientManager &#123;</span><br><span class="line">	result := &amp;clientManager&#123;</span><br><span class="line">		kubeConfigPath: kubeConfigPath,</span><br><span class="line">		apiserverHost:  apiserverHost,</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	// 执行初始化工作</span><br><span class="line">	result.init() </span><br><span class="line">	return result</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><figcaption><span>/client/manager.go[+294]</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">func (self *clientManager) init() &#123;</span><br><span class="line">	// 如果没有配置apiserverHost and kubeConfigPath就执行这个初始化操作</span><br><span class="line">	self.initInClusterConfig()</span><br><span class="line">	</span><br><span class="line">	self.initCSRFKey()</span><br><span class="line">	self.initInsecureClient()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/09/02/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/09/02/1.html" itemprop="url">带你学透红黑树</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-02T23:01:47+08:00">
                2018-09-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">数据结构</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/数据结构/红黑树/" itemprop="url" rel="index">
                    <span itemprop="name">红黑树</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/09/02/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/09/02/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/09/02/1.html" class="leancloud_visitors" data-flag-title="带你学透红黑树">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="红黑树"><a href="#红黑树" class="headerlink" title="红黑树"></a>红黑树</h1><h2 id="什么是红黑树"><a href="#什么是红黑树" class="headerlink" title="什么是红黑树"></a>什么是红黑树</h2><p>红黑树（Red Black Tree） 是一种自平衡二叉查找树，是在计算机科学中用到的一种数据结构，典型的用途是实现关联数组。</p>
<h2 id="红黑树的特征"><a href="#红黑树的特征" class="headerlink" title="红黑树的特征"></a>红黑树的特征</h2><ol>
<li>每个节点被标记为黑色或者红色</li>
<li>根节点总是黑色的</li>
<li>红色节点的子节点必须是黑色节点（反之没有要求）</li>
<li>从根节点到叶节点的路径，包含的黑色节点数目必须完全相等</li>
</ol>
<h2 id="红黑树的操作集合"><a href="#红黑树的操作集合" class="headerlink" title="红黑树的操作集合"></a>红黑树的操作集合</h2><ol>
<li>添加</li>
<li>删除</li>
<li>旋转</li>
</ol>
<p>从红黑树中添加或删除节点时，我们需要修正红黑树，以满足约束</p>
<h3 id="节点结构"><a href="#节点结构" class="headerlink" title="节点结构"></a>节点结构</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">RBTreeNode</span> &#123;</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span>       color;</span><br><span class="line">    Type                key;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">RBTreeNode</span>   *<span class="title">left</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">RBTreeNode</span>   *<span class="title">right</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">RBTreeNode</span>   *<span class="title">parent</span>;</span></span><br><span class="line">&#125; Node, *RBTree;</span><br></pre></td></tr></table></figure>
<h3 id="旋转"><a href="#旋转" class="headerlink" title="旋转"></a>旋转</h3><p>旋转是为了维持树的平衡</p>
<p><strong>左旋</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> *         P                     P</span></span><br><span class="line"><span class="comment"> *        /                     /</span></span><br><span class="line"><span class="comment"> *       x                     y</span></span><br><span class="line"><span class="comment"> *      / \                   / \</span></span><br><span class="line"><span class="comment"> *     lx  y      ------&gt;    x   ry</span></span><br><span class="line"><span class="comment"> *     / \                  / \</span></span><br><span class="line"><span class="comment"> *    ly ry                lx  ly</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">leftRotate</span><span class="params">(Node *x)</span> </span>&#123;</span><br><span class="line">    Node *y = x-&gt;right;</span><br><span class="line">    <span class="comment">// 处理ry节点</span></span><br><span class="line">    x-&gt;right = y-&gt;left;</span><br><span class="line">    <span class="keyword">if</span> (y-&gt;left != <span class="literal">NULL</span>) &#123;</span><br><span class="line">        y-&gt;left-&gt;parent = x;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 处理y节点</span></span><br><span class="line">    y-&gt;parent = x-&gt;parent;</span><br><span class="line">    <span class="keyword">if</span> (x-&gt;parent == <span class="literal">NULL</span>) &#123;</span><br><span class="line">        <span class="comment">// 将y设为root</span></span><br><span class="line">        RBTree = y;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// x为左节点</span></span><br><span class="line">        <span class="keyword">if</span> (x == x-&gt;parent-&gt;left) &#123;</span><br><span class="line">            x-&gt;parent-&gt;left = y;</span><br><span class="line">        <span class="comment">// x为右节点</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            x-&gt;parent-&gt;right = y;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 处理x节点</span></span><br><span class="line">    x-&gt;parent = y;</span><br><span class="line">    y-&gt;left = x;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>右旋</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> *              p                     p</span></span><br><span class="line"><span class="comment"> *             /                     /</span></span><br><span class="line"><span class="comment"> *            y                     x</span></span><br><span class="line"><span class="comment"> *           / \      ------&gt;      / \</span></span><br><span class="line"><span class="comment"> *          x   ry                lx  y</span></span><br><span class="line"><span class="comment"> *         / \                       / \</span></span><br><span class="line"><span class="comment"> *        lx  rx                    rx  ry</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rightRotate</span><span class="params">(Node *y)</span> </span>&#123;</span><br><span class="line">    Node *x = y-&gt;left;</span><br><span class="line">    <span class="comment">// 处理rx节点</span></span><br><span class="line">    y-&gt;left = x-&gt;right;</span><br><span class="line">    <span class="keyword">if</span> (x-&gt;right != <span class="literal">NULL</span>) &#123;</span><br><span class="line">        x-&gt;right-&gt;parent = y;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 处理x节点</span></span><br><span class="line">    x-&gt;parent = y-&gt;parent;</span><br><span class="line">    <span class="keyword">if</span> (x-&gt;parent == <span class="literal">NULL</span>) &#123;</span><br><span class="line">        RBTree = x;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (y == y-&gt;parent-&gt;left) &#123;</span><br><span class="line">            y-&gt;parent-&gt;left = x;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            y-&gt;parent-&gt;right = x;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 处理y节点</span></span><br><span class="line">    y-&gt;parent = x;</span><br><span class="line">    x-&gt;right = y;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="插入操作"><a href="#插入操作" class="headerlink" title="插入操作"></a>插入操作</h3><p>插入操作相对于二叉搜索树来说，就是在其基础上添加了节点的平衡维护工作，因此大部分的操作都是与二叉搜索树一致</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">void insertNode(Node *node) &#123;</span><br><span class="line">    Node *current = NULL;</span><br><span class="line">    Node *x = RBTree;</span><br><span class="line">    int cmp = 0;</span><br><span class="line">    <span class="keyword">while</span> (x != NULL) &#123;</span><br><span class="line">        current = x;</span><br><span class="line">        cmp = compare(node, current);</span><br><span class="line">        <span class="keyword">if</span> (cmp &lt; 0) &#123;</span><br><span class="line">            x = x-&gt;left;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            x = x-&gt;right;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    node.parent = current;</span><br><span class="line">    <span class="keyword">if</span> (current != NULL) &#123;</span><br><span class="line">        <span class="keyword">if</span> (cmp &lt; 0) &#123;</span><br><span class="line">            current-&gt;left = node;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            current-&gt;right = node;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        RBTree = node;</span><br><span class="line">    &#125;</span><br><span class="line">    insertFixUp(RBTree);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="关于节点修正"><a href="#关于节点修正" class="headerlink" title="关于节点修正"></a>关于节点修正</h3><p>为了满足红黑树的规则，我们需要处理如下的情况：</p>
<ol>
<li><p>第一次插入，由于原树为空，这样只会违背第一条规则（根节点是黑色节点）</p>
<p> 将插入节点从红色改为黑色</p>
</li>
<li><p>插入节点的父节点是黑色节点</p>
<p> 不需要做什么</p>
</li>
<li><p>插入节点父节点与叔叔节点为红色</p>
<ol>
<li>将父节点与叔叔节点都涂黑</li>
<li>将祖父节点涂红</li>
<li>将当前节点指向祖父节点，继续</li>
</ol>
</li>
<li><p>插入节点的父节点是红色，叔叔节点是黑色，且插入节点是其父节点的右子节点</p>
<ol>
<li>将当前节点的父节点作为当前节点</li>
<li>以新的当前节点为支点进行左旋操作</li>
<li>这时变成了情况5</li>
</ol>
</li>
<li><p>插入节点的父节点是红色，叔叔节点是黑色，且插入节点是其父节点的左子节点</p>
<ol>
<li>将父节点涂黑，祖父节点涂红</li>
<li>以祖父节点为支点做右旋操作</li>
<li>最后将根节点涂黑</li>
</ol>
</li>
</ol>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">insertFixUp</span><span class="params">(Node *node)</span> </span>&#123;</span><br><span class="line">    Node *parent, *gparent;</span><br><span class="line">    <span class="keyword">while</span>((parent = parentOf(node)) != <span class="literal">NULL</span> &amp;&amp; isRed(parent)) &#123;</span><br><span class="line">        gparent = parentOf(parent);</span><br><span class="line">        <span class="keyword">if</span> (parent == gparent.left) &#123;</span><br><span class="line">            Node *uncle = gparent-&gt;right;</span><br><span class="line">            <span class="keyword">if</span> (uncle != <span class="literal">NULL</span> &amp;&amp; isRed(uncle)) &#123;</span><br><span class="line">                setBlack(parent);</span><br><span class="line">                setBlack(uncle);</span><br><span class="line">                setBlack(gparent);</span><br><span class="line">                node = gparent;</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 由于红色节点的子节点一定是黑色，因此当前节点的uncle节点一定存在</span></span><br><span class="line">            <span class="keyword">if</span> (node == parent-&gt;right) &#123;</span><br><span class="line">                node = parent;</span><br><span class="line">                leftRotate(node);</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                setBlack(parent);</span><br><span class="line">                setRed(gparent);</span><br><span class="line">                rightRotate(gparent);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            Node *uncle = gparent-&gt;left;</span><br><span class="line">            <span class="keyword">if</span> (uncle != <span class="literal">NULL</span> &amp;&amp; isRed(uncle)) &#123;</span><br><span class="line">                setBlack(parent);</span><br><span class="line">                setBlack(uncle);</span><br><span class="line">                setBlack(gparent);</span><br><span class="line">                node = gparent;</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (node == parent-&gt;left) &#123;</span><br><span class="line">                node = parent;</span><br><span class="line">                rightRotate(node);</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                setBlack(parent);</span><br><span class="line">                setRed(gparent);</span><br><span class="line">                leftRotate(gparent);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    setBlack(root);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangxiaochuang.github.io/blog/2018/08/31/1.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jackstraw">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="稻草人的编程之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/31/1.html" itemprop="url">hexo使用教程</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-31T20:04:09+08:00">
                2018-08-31
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/其他/" itemprop="url" rel="index">
                    <span itemprop="name">其他</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/08/31/1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/blog/2018/08/31/1.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/blog/2018/08/31/1.html" class="leancloud_visitors" data-flag-title="hexo使用教程">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="Hexo-的快速解释"><a href="#Hexo-的快速解释" class="headerlink" title="Hexo 的快速解释"></a>Hexo 的快速解释</h1><p>Hexo 是一个快速、简洁且高效的博客框架。Hex将Markdown文件渲染为html文件，几秒之内就能创建一个漂亮的静态网页</p>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>安装Hexo非常简单，只依赖与<code>Node.js</code>与<code>Git</code></p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g hexo-cli</span><br></pre></td></tr></table></figure>
<h2 id="开始建站"><a href="#开始建站" class="headerlink" title="开始建站"></a>开始建站</h2><ol>
<li>初始化项目</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hexo init &lt;folder&gt;</span><br><span class="line"><span class="built_in">cd</span> &lt;folder&gt;</span><br><span class="line">npm install</span><br></pre></td></tr></table></figure>
<ol>
<li>启用测试服务进行测试</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 默认打开4000端口</span></span><br><span class="line">hexo server</span><br></pre></td></tr></table></figure>
<h2 id="结合travis-ci部署博客到github-pages"><a href="#结合travis-ci部署博客到github-pages" class="headerlink" title="结合travis ci部署博客到github pages"></a>结合travis ci部署博客到github pages</h2><ol>
<li>使用github账号登录 <a href="https://travis-ci.org/">travis ci</a></li>
</ol>
<p>登录后默认会同步github的所有仓库，如果没有找到自己的仓库可以手动点击同步</p>
<ol>
<li>进入博客项目的设置界面，使用默认的配置，只需要添加一个<code>Environment Variables</code></li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">key: GH_TOKEN</span><br><span class="line">value: &lt;github的访问token&gt;</span><br></pre></td></tr></table></figure>
<ol>
<li>在博客项目根目录添加<code>.travis.yml</code>文件</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">language: node_js</span><br><span class="line">node_js: stable</span><br><span class="line">script:</span><br><span class="line">  - hexo g</span><br><span class="line">deploy:</span><br><span class="line">  provider: pages</span><br><span class="line">  local-dir: public</span><br><span class="line">  skip-cleanup: true</span><br><span class="line">  github-token: $GH_TOKEN</span><br><span class="line">  keep-history: true</span><br><span class="line">  on:</span><br><span class="line">    branch: master</span><br></pre></td></tr></table></figure>
<ol>
<li>现在向博客系统提交项目就会触发部署</li>
</ol>
<p>travis-ci已经适配了github-pages服务，默认会将编译后的博客文件发布到<code>gh-pages</code>分支。</p>
<p>拥有<code>gh-pages</code>的github项目，默认会开启pages服务，访问地址<code>https://&lt;github账号&gt;.github.io/&lt;项目名&gt;/</code></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/blog/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/blog/">1</a><span class="page-number current">2</span>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpg"
                alt="jackstraw" />
            
              <p class="site-author-name" itemprop="name">jackstraw</p>
              <p class="site-description motion-element" itemprop="description">人生的意义，不在于最终获得了什么，而在于曾经努力追求过什么</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">jackstraw</span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>访问人数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: true,
        notify: false,
        appId: 'lQM75w94ggNR0TjX61NLerrg-gzGzoHsz',
        appKey: 'I2wtQ2rd9KtoJmcEOiYG9zqT',
        placeholder: '如需帮助，请留下邮箱',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
    var infoEle = document.querySelector('#comments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0){
      infoEle.childNodes.forEach(function(item) {
        item.parentNode.removeChild(item);
      });
    }
  </script>



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("lQM75w94ggNR0TjX61NLerrg-gzGzoHsz", "I2wtQ2rd9KtoJmcEOiYG9zqT");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
